]> granicus.if.org Git - vim/commitdiff
patch 8.2.3741: using freed memory in open command v8.2.3741
authorBram Moolenaar <Bram@vim.org>
Sun, 5 Dec 2021 12:06:24 +0000 (12:06 +0000)
committerBram Moolenaar <Bram@vim.org>
Sun, 5 Dec 2021 12:06:24 +0000 (12:06 +0000)
Problem:    Using freed memory in open command.
Solution:   Make a copy of the current line.

src/ex_docmd.c
src/testdir/test_ex_mode.vim
src/version.c

index 439a78cedc5d07f689dd572f57289c0007c91de2..1fc538b85c50df3bf7613abde0265d587e4f8108 100644 (file)
@@ -6877,13 +6877,17 @@ ex_open(exarg_T *eap)
        regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
        if (regmatch.regprog != NULL)
        {
+           // make a copy of the line, when searching for a mark it might be
+           // flushed
+           char_u *line = vim_strsave(ml_get_curline());
+
            regmatch.rm_ic = p_ic;
-           p = ml_get_curline();
-           if (vim_regexec(&regmatch, p, (colnr_T)0))
-               curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
+           if (vim_regexec(&regmatch, line, (colnr_T)0))
+               curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
            else
                emsg(_(e_nomatch));
            vim_regfree(regmatch.regprog);
+           vim_free(line);
        }
        // Move to the NUL, ignore any other arguments.
        eap->arg += STRLEN(eap->arg);
index 1ca5062328679b8a468baec8af92f3e69a16fa44..7031115fcba5a8dbe0f51b837f7df08adee78844 100644 (file)
@@ -121,6 +121,19 @@ func Test_open_command()
   close!
 endfunc
 
+func Test_open_command_flush_line()
+  " this was accessing freed memory: the regexp match uses a pointer to the
+  " current line which becomes invalid when searching for the ') mark.
+  new
+  call setline(1, ['one', 'two. three'])
+  s/one/ONE
+  try
+    open /\%')/
+  catch /E479/
+  endtry
+  bwipe!
+endfunc
+
 " Test for :g/pat/visual to run vi commands in Ex mode
 " This used to hang Vim before 8.2.0274.
 func Test_Ex_global()
index 6e1e30aa9870f94ff636534d4c2fcdbdbb16dd4d..83326fef65e7b0586c6556ac344dbdcf2606c56a 100644 (file)
@@ -753,6 +753,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3741,
 /**/
     3740,
 /**/