Skip to content

Commit

Permalink
patch 8.2.4977: memory access error when substitute expression change…
Browse files Browse the repository at this point in the history
…s window

Problem:    Memory access error when substitute expression changes window.
Solution:   Disallow changing window in substitute expression.
  • Loading branch information
brammool committed May 18, 2022
1 parent b1040dc commit e2bd860
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/ex_cmds.c
Expand Up @@ -4412,12 +4412,17 @@ ex_substitute(exarg_T *eap)
// Save flags for recursion. They can change for e.g.
// :s/^/\=execute("s#^##gn")
subflags_save = subflags;

// Disallow changing text or switching window in an expression.
++textwinlock;
#endif
// get length of substitution part
sublen = vim_regsub_multi(&regmatch,
sub_firstlnum - regmatch.startpos[0].lnum,
sub, sub_firstline, FALSE, magic_isset(), TRUE);
#ifdef FEAT_EVAL
--textwinlock;

// If getting the substitute string caused an error, don't do
// the replacement.
// Don't keep flags set by a recursive call.
Expand Down Expand Up @@ -4518,9 +4523,15 @@ ex_substitute(exarg_T *eap)
mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
new_end += copy_len;

#ifdef FEAT_EVAL
++textwinlock;
#endif
(void)vim_regsub_multi(&regmatch,
sub_firstlnum - regmatch.startpos[0].lnum,
sub, new_end, TRUE, magic_isset(), TRUE);
#ifdef FEAT_EVAL
--textwinlock;
#endif
sub_nsubs++;
did_sub = TRUE;

Expand Down
13 changes: 13 additions & 0 deletions src/testdir/test_substitute.vim
Expand Up @@ -1000,6 +1000,19 @@ func Test_using_old_sub()
set nocompatible
endfunc

" This was switching windows in between computing the length and using it.
func Test_sub_change_window()
silent! lfile
sil! norm o0000000000000000000000000000000000000000000000000000
func Repl()
lopen
endfunc
silent! s/\%')/\=Repl()
bwipe!
bwipe!
delfunc Repl
endfunc

" Test for the 2-letter and 3-letter :substitute commands
func Test_substitute_short_cmd()
new
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -746,6 +746,8 @@ static char *(features[]) =

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

0 comments on commit e2bd860

Please sign in to comment.