Skip to content

Commit d77f9d5

Browse files
committed
patch 7.4.2323
Problem: Using freed memory when using 'formatexpr'. (Dominique Pelle) Solution: Make a copy of 'formatexpr' before evaluating it.
1 parent bc54f3f commit d77f9d5

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/ops.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4741,6 +4741,7 @@ fex_format(
47414741
int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
47424742
OPT_LOCAL);
47434743
int r;
4744+
char_u *fex;
47444745

47454746
/*
47464747
* Set v:lnum to the first line number and v:count to the number of lines.
@@ -4750,16 +4751,22 @@ fex_format(
47504751
set_vim_var_nr(VV_COUNT, count);
47514752
set_vim_var_char(c);
47524753

4754+
/* Make a copy, the option could be changed while calling it. */
4755+
fex = vim_strsave(curbuf->b_p_fex);
4756+
if (fex == NULL)
4757+
return 0;
4758+
47534759
/*
47544760
* Evaluate the function.
47554761
*/
47564762
if (use_sandbox)
47574763
++sandbox;
4758-
r = (int)eval_to_number(curbuf->b_p_fex);
4764+
r = (int)eval_to_number(fex);
47594765
if (use_sandbox)
47604766
--sandbox;
47614767

47624768
set_vim_var_string(VV_CHAR, NULL, -1);
4769+
vim_free(fex);
47634770

47644771
return r;
47654772
}

src/testdir/test_normal.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,30 @@ func! Test_normal05_formatexpr()
192192
bw!
193193
endfu
194194

195+
func Test_normal05_formatexpr_newbuf()
196+
" Edit another buffer in the 'formatexpr' function
197+
new
198+
func! Format()
199+
edit another
200+
endfunc
201+
set formatexpr=Format()
202+
norm gqG
203+
bw!
204+
set formatexpr=
205+
endfunc
206+
207+
func Test_normal05_formatexpr_setopt()
208+
" Change the 'formatexpr' value in the function
209+
new
210+
func! Format()
211+
set formatexpr=
212+
endfunc
213+
set formatexpr=Format()
214+
norm gqG
215+
bw!
216+
set formatexpr=
217+
endfunc
218+
195219
func! Test_normal06_formatprg()
196220
" basic test for formatprg
197221
" only test on non windows platform

src/version.c

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

764764
static int included_patches[] =
765765
{ /* Add new patch number below this line */
766+
/**/
767+
2323,
766768
/**/
767769
2322,
768770
/**/

0 commit comments

Comments
 (0)