]> granicus.if.org Git - vim/commitdiff
patch 8.2.0817: not enough memory allocated when converting string v8.2.0817
authorBram Moolenaar <Bram@vim.org>
Sun, 24 May 2020 16:45:07 +0000 (18:45 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 24 May 2020 16:45:07 +0000 (18:45 +0200)
Problem:    Not enough memory allocated when converting string with special
            character.
Solution:   Reserve space for modifier code. (closes #6130)

src/eval.c
src/testdir/test_functions.vim
src/version.c

index dbc10c1b69c097323601f65665dd63e09c43da8e..00b6c59df2a83ee8e66ed4ca988004939b815d5b 100644 (file)
@@ -3503,6 +3503,7 @@ get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
     char_u     *p;
     char_u     *name;
     int                extra = 0;
+    int                len;
 
     /*
      * Find the end of the string, skipping backslashed characters.
@@ -3513,9 +3514,10 @@ get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
        {
            ++p;
            // A "\<x>" form occupies at least 4 characters, and produces up
-           // to 6 characters: reserve space for 2 extra
+           // to 9 characters (6 for the char and 3 for a modifier): reserve
+           // space for 5 extra.
            if (*p == '<')
-               extra += 2;
+               extra += 5;
        }
     }
 
@@ -3536,7 +3538,8 @@ get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
      * Copy the string into allocated memory, handling backslashed
      * characters.
      */
-    name = alloc(p - *arg + extra);
+    len = (int)(p - *arg + extra);
+    name = alloc(len);
     if (name == NULL)
        return FAIL;
     rettv->v_type = VAR_STRING;
@@ -3610,6 +3613,8 @@ get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
                          if (extra != 0)
                          {
                              name += extra;
+                             if (name >= rettv->vval.v_string + len)
+                                 iemsg("get_string_tv() used more space than allocated");
                              break;
                          }
                          // FALLTHROUGH
index a8506a8f765ea55df0ace5da6e99072f3961fe7b..712aec4eae2e20c2c4f210c806050ab100b454aa 100644 (file)
@@ -2278,6 +2278,8 @@ func Test_nr2char()
   set encoding=utf8
   call assert_equal('a', nr2char(97, 1))
   call assert_equal('a', nr2char(97, 0))
+
+  call assert_equal("\x80\xfc\b\xf4\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x100000) .. '>"'))
 endfunc
 
 " Test for screenattr(), screenchar() and screenchars() functions
index f2afc0b443d2f18278270286af81d295164154c2..94084c30d5365dd8c983360853f3ba74220d94d3 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    817,
 /**/
     816,
 /**/