0
0
mirror of https://github.com/vim/vim.git synced 2025-10-04 05:25:06 -04:00

patch 8.0.0667: memory access error when command follows :endfunc

Problem:    Memory access error when command follows :endfunction. (Nikolai
            Pavlov)
Solution:   Make memory handling in :function straightforward. (closes #1793)
This commit is contained in:
Bram Moolenaar
2017-06-24 14:48:11 +02:00
parent 5fe691240b
commit 53564f7c1a
3 changed files with 42 additions and 21 deletions

View File

@@ -1379,6 +1379,11 @@ func Test_endfunction_trailing()
delfunc Xtest delfunc Xtest
unlet done unlet done
" trailing line break
exe "func Xtest()\necho 'hello'\nendfunc\n"
call assert_true(exists('*Xtest'))
delfunc Xtest
set verbose=1 set verbose=1
exe "func Xtest()\necho 'hello'\nendfunc \" garbage" exe "func Xtest()\necho 'hello'\nendfunc \" garbage"
call assert_notmatch('W22:', split(execute('1messages'), "\n")[0]) call assert_notmatch('W22:', split(execute('1messages'), "\n")[0])
@@ -1390,6 +1395,11 @@ func Test_endfunction_trailing()
call assert_true(exists('*Xtest')) call assert_true(exists('*Xtest'))
delfunc Xtest delfunc Xtest
set verbose=0 set verbose=0
function Foo()
echo 'hello'
endfunction | echo 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
delfunc Foo
endfunc endfunc
func Test_delfunction_force() func Test_delfunction_force()

View File

@@ -1780,6 +1780,7 @@ theend:
ex_function(exarg_T *eap) ex_function(exarg_T *eap)
{ {
char_u *theline; char_u *theline;
char_u *line_to_free = NULL;
int j; int j;
int c; int c;
int saved_did_emsg; int saved_did_emsg;
@@ -2093,10 +2094,15 @@ ex_function(exarg_T *eap)
line_arg = p + 1; line_arg = p + 1;
} }
} }
else if (eap->getline == NULL) else
{
vim_free(line_to_free);
if (eap->getline == NULL)
theline = getcmdline(':', 0L, indent); theline = getcmdline(':', 0L, indent);
else else
theline = eap->getline(':', eap->cookie, indent); theline = eap->getline(':', eap->cookie, indent);
line_to_free = theline;
}
if (KeyTyped) if (KeyTyped)
lines_left = Rows - 1; lines_left = Rows - 1;
if (theline == NULL) if (theline == NULL)
@@ -2130,18 +2136,29 @@ ex_function(exarg_T *eap)
/* Check for "endfunction". */ /* Check for "endfunction". */
if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0) if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
{ {
char_u *nextcmd = NULL;
if (*p == '|') if (*p == '|')
/* Another command follows. */ nextcmd = p + 1;
eap->nextcmd = vim_strsave(p + 1);
else if (line_arg != NULL && *skipwhite(line_arg) != NUL) else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
/* Another command follows. */ nextcmd = line_arg;
eap->nextcmd = line_arg;
else if (*p != NUL && *p != '"' && p_verbose > 0) else if (*p != NUL && *p != '"' && p_verbose > 0)
give_warning2( give_warning2(
(char_u *)_("W22: Text found after :endfunction: %s"), (char_u *)_("W22: Text found after :endfunction: %s"),
p, TRUE); p, TRUE);
if (line_arg == NULL) if (nextcmd != NULL)
vim_free(theline); {
/* Another command follows. If the line came from "eap" we
* can simply point into it, otherwise we need to change
* "eap->cmdlinep". */
eap->nextcmd = nextcmd;
if (line_to_free != NULL)
{
vim_free(*eap->cmdlinep);
*eap->cmdlinep = line_to_free;
line_to_free = NULL;
}
}
break; break;
} }
@@ -2212,24 +2229,15 @@ ex_function(exarg_T *eap)
/* Add the line to the function. */ /* Add the line to the function. */
if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL) if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
{
if (line_arg == NULL)
vim_free(theline);
goto erret; goto erret;
}
/* Copy the line to newly allocated memory. get_one_sourceline() /* Copy the line to newly allocated memory. get_one_sourceline()
* allocates 250 bytes per line, this saves 80% on average. The cost * allocates 250 bytes per line, this saves 80% on average. The cost
* is an extra alloc/free. */ * is an extra alloc/free. */
p = vim_strsave(theline); p = vim_strsave(theline);
if (p != NULL) if (p == NULL)
{ goto erret;
if (line_arg == NULL) ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
vim_free(theline);
theline = p;
}
((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
/* Add NULL lines for continuation lines, so that the line count is /* Add NULL lines for continuation lines, so that the line count is
* equal to the index in the growarray. */ * equal to the index in the growarray. */
@@ -2428,6 +2436,7 @@ errret_2:
ga_clear_strings(&newlines); ga_clear_strings(&newlines);
ret_free: ret_free:
vim_free(skip_until); vim_free(skip_until);
vim_free(line_to_free);
vim_free(fudi.fd_newkey); vim_free(fudi.fd_newkey);
vim_free(name); vim_free(name);
did_emsg |= saved_did_emsg; did_emsg |= saved_did_emsg;

View File

@@ -764,6 +764,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
667,
/**/ /**/
666, 666,
/**/ /**/