Skip to content

Commit 6de2296

Browse files
committed
patch 9.0.0432: crash when using for loop variable in closure
Problem: Crash when using for loop variable in closure. Solution: Check that the variable wasn't deleted. (issue #11094)
1 parent 7cf5839 commit 6de2296

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,4 +3331,6 @@ EXTERN char e_cannot_use_partial_with_dictionary_for_defer[]
33313331
INIT(= N_("E1300: Cannot use a partial with dictionary for :defer"));
33323332
EXTERN char e_string_number_list_or_blob_required_for_argument_nr[]
33333333
INIT(= N_("E1301: String, Number, List or Blob required for argument %d"));
3334+
EXTERN char e_script_variable_was_deleted[]
3335+
INIT(= N_("E1302: Script variable was deleted"));
33343336
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
|~+0#4040ff13#ffffff0| @73
2+
|~| @73
3+
|E+0#ffffff16#e000002|r@1|o|r| |d|e|t|e|c|t|e|d| |w|h|i|l|e| |p|r|o|c|e|s@1|i|n|g| |f|u|n|c|t|i|o|n| |<|l|a|m|b|d|a|>|1|:| +0#0000000#ffffff0@23
4+
|l+0#af5f00255&|i|n|e| @3|1|:| +0#0000000&@64
5+
|E+0#ffffff16#e000002|1|3|0|2|:| |S|c|r|i|p|t| |v|a|r|i|a|b|l|e| |w|a|s| |d|e|l|e|t|e|d| +0#0000000#ffffff0@40
6+
|P+0#00e0003&|r|e|s@1| |E|N|T|E|R| |o|r| |t|y|p|e| |c|o|m@1|a|n|d| |t|o| |c|o|n|t|i|n|u|e> +0#0000000&@35

src/testdir/test_vim9_func.vim

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,6 +2943,30 @@ def Test_nested_closure_fails()
29432943
v9.CheckScriptFailure(lines, 'E1012:')
29442944
enddef
29452945

2946+
def Run_Test_closure_in_for_loop_fails()
2947+
var lines =<< trim END
2948+
vim9script
2949+
for n in [0]
2950+
timer_start(10, (_) => {
2951+
echo n
2952+
})
2953+
endfor
2954+
END
2955+
writefile(lines, 'XTest_closure_fails', 'D')
2956+
2957+
# Check that an error shows
2958+
var buf = g:RunVimInTerminal('-S XTest_closure_fails', {'rows': 6})
2959+
g:VerifyScreenDump(buf, 'Test_vim9_closure_fails', {})
2960+
2961+
# clean up
2962+
g:StopVimInTerminal(buf)
2963+
enddef
2964+
2965+
func Test_closure_in_for_loop_fails()
2966+
CheckScreendump
2967+
call Run_Test_closure_in_for_loop_fails()
2968+
endfunc
2969+
29462970
def Test_global_closure()
29472971
var lines =<< trim END
29482972
vim9script
@@ -3321,7 +3345,7 @@ def Run_Test_silent_echo()
33213345
enddef
33223346
defcompile
33233347
END
3324-
writefile(lines, 'XTest_silent_echo')
3348+
writefile(lines, 'XTest_silent_echo', 'D')
33253349

33263350
# Check that the balloon shows up after a mouse move
33273351
var buf = g:RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
@@ -3330,7 +3354,6 @@ def Run_Test_silent_echo()
33303354

33313355
# clean up
33323356
g:StopVimInTerminal(buf)
3333-
delete('XTest_silent_echo')
33343357
enddef
33353358

33363359
def SilentlyError()

src/version.c

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

704704
static int included_patches[] =
705705
{ /* Add new patch number below this line */
706+
/**/
707+
432,
706708
/**/
707709
431,
708710
/**/

src/vim9execute.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,12 @@ get_script_svar(scriptref_T *sref, int dfunc_idx)
17161716
return NULL;
17171717
}
17181718
sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1719+
if (sv->sv_name == NULL)
1720+
{
1721+
if (dfunc != NULL)
1722+
emsg(_(e_script_variable_was_deleted));
1723+
return NULL;
1724+
}
17191725
if (!equal_type(sv->sv_type, sref->sref_type, 0))
17201726
{
17211727
if (dfunc != NULL)

0 commit comments

Comments
 (0)