Skip to content

Commit 32acf1f

Browse files
committed
patch 9.0.0047: using freed memory with recursive substitute
Problem: Using freed memory with recursive substitute. Solution: Always make a copy for reg_prev_sub.
1 parent baefde1 commit 32acf1f

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/ex_cmds.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3994,7 +3994,16 @@ ex_substitute(exarg_T *eap)
39943994
sub_copy = sub;
39953995
}
39963996
else
3997-
sub = regtilde(sub, magic_isset());
3997+
{
3998+
char_u *newsub = regtilde(sub, magic_isset());
3999+
4000+
if (newsub != sub)
4001+
{
4002+
// newsub was allocated, free it later.
4003+
sub_copy = newsub;
4004+
sub = newsub;
4005+
}
4006+
}
39984007

39994008
/*
40004009
* Check for a match on each line.

src/regexp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,11 +1766,11 @@ regtilde(char_u *source, int magic)
17661766
}
17671767
}
17681768

1769+
// Store a copy of newsub in reg_prev_sub. It is always allocated,
1770+
// because recursive calls may make the returned string invalid.
17691771
vim_free(reg_prev_sub);
1770-
if (newsub != source) // newsub was allocated, just keep it
1771-
reg_prev_sub = newsub;
1772-
else // no ~ found, need to save newsub
1773-
reg_prev_sub = vim_strsave(newsub);
1772+
reg_prev_sub = vim_strsave(newsub);
1773+
17741774
return newsub;
17751775
}
17761776

src/testdir/test_regexp_latin.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,4 +1114,15 @@ func Test_using_two_engines_pattern()
11141114
bwipe!
11151115
endfunc
11161116

1117+
func Test_recursive_substitute_expr()
1118+
new
1119+
func Repl()
1120+
s
1121+
endfunc
1122+
silent! s/\%')/~\=Repl()
1123+
1124+
bwipe!
1125+
delfunc Repl
1126+
endfunc
1127+
11171128
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ static char *(features[]) =
735735

736736
static int included_patches[] =
737737
{ /* Add new patch number below this line */
738+
/**/
739+
47,
738740
/**/
739741
46,
740742
/**/

0 commit comments

Comments
 (0)