Skip to content

Commit

Permalink
patch 9.0.0047: using freed memory with recursive substitute
Browse files Browse the repository at this point in the history
Problem:    Using freed memory with recursive substitute.
Solution:   Always make a copy for reg_prev_sub.
  • Loading branch information
brammool committed Jul 7, 2022
1 parent baefde1 commit 32acf1f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/ex_cmds.c
Expand Up @@ -3994,7 +3994,16 @@ ex_substitute(exarg_T *eap)
sub_copy = sub;
}
else
sub = regtilde(sub, magic_isset());
{
char_u *newsub = regtilde(sub, magic_isset());

if (newsub != sub)
{
// newsub was allocated, free it later.
sub_copy = newsub;
sub = newsub;
}
}

/*
* Check for a match on each line.
Expand Down
8 changes: 4 additions & 4 deletions src/regexp.c
Expand Up @@ -1766,11 +1766,11 @@ regtilde(char_u *source, int magic)
}
}

// Store a copy of newsub in reg_prev_sub. It is always allocated,
// because recursive calls may make the returned string invalid.
vim_free(reg_prev_sub);
if (newsub != source) // newsub was allocated, just keep it
reg_prev_sub = newsub;
else // no ~ found, need to save newsub
reg_prev_sub = vim_strsave(newsub);
reg_prev_sub = vim_strsave(newsub);

return newsub;
}

Expand Down
11 changes: 11 additions & 0 deletions src/testdir/test_regexp_latin.vim
Expand Up @@ -1114,4 +1114,15 @@ func Test_using_two_engines_pattern()
bwipe!
endfunc

func Test_recursive_substitute_expr()
new
func Repl()
s
endfunc
silent! s/\%')/~\=Repl()

bwipe!
delfunc Repl
endfunc

" vim: shiftwidth=2 sts=2 expandtab
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -735,6 +735,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
47,
/**/
46,
/**/
Expand Down

0 comments on commit 32acf1f

Please sign in to comment.