summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-09-17 21:29:03 +0200
committerBram Moolenaar <Bram@vim.org>2020-09-17 21:29:03 +0200
commit77e5dcc36a82da040072d74e3ced410d15c42757 (patch)
tree00e7367a7cb2433b07a72fa976e0c528c7f77fe0
parent213da551dec465e193619684b260bf9d5a8d6afc (diff)
downloadvim-git-77e5dcc36a82da040072d74e3ced410d15c42757.tar.gz
patch 8.2.1704: Vim9: crash in for loop when autoload script has an errorv8.2.1704
Problem: Vim9: crash in for loop when autoload script has an error. Solution: Reset suppress_errthrow. Check for NULL list. (closes #6967)
-rw-r--r--src/testdir/test_vim9_script.vim39
-rw-r--r--src/version.c2
-rw-r--r--src/vim9execute.c10
3 files changed, 50 insertions, 1 deletions
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 2f1e708f2..42eaab0ad 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -3353,6 +3353,45 @@ def Test_vim9_autoload()
&rtp = save_rtp
enddef
+" This was causing a crash because suppress_errthrow wasn't reset.
+def Test_vim9_autoload_error()
+ let lines =<< trim END
+ vim9script
+ def crash#func()
+ try
+ for x in List()
+ endfor
+ catch
+ endtry
+ g:ok = true
+ enddef
+ fu List()
+ invalid
+ endfu
+ try
+ invalid
+ catch /wontmatch/
+ endtry
+ END
+ call mkdir('Xruntime/autoload', 'p')
+ call writefile(lines, 'Xruntime/autoload/crash.vim')
+
+ # run in a separate Vim to avoid the side effects of assert_fails()
+ lines =<< trim END
+ exe 'set rtp^=' .. getcwd() .. '/Xruntime'
+ call crash#func()
+ call writefile(['ok'], 'Xdidit')
+ qall
+ END
+ writefile(lines, 'Xscript')
+ RunVim([], [], '-S Xscript')
+ assert_equal(['ok'], readfile('Xdidit'))
+
+ delete('Xdidit')
+ delete('Xscript')
+ delete('Xruntime', 'rf')
+enddef
+
def Test_script_var_in_autocmd()
# using a script variable from an autocommand, defined in a :def function in a
# legacy Vim script, cannot check the variable type.
diff --git a/src/version.c b/src/version.c
index f7e172051..0e52351ea 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1704,
+/**/
1703,
/**/
1702,
diff --git a/src/vim9execute.c b/src/vim9execute.c
index 40d6193ca..4332c03ed 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -761,6 +761,7 @@ call_def_function(
sctx_T save_current_sctx = current_sctx;
int breakcheck_count = 0;
int called_emsg_before = called_emsg;
+ int save_suppress_errthrow = suppress_errthrow;
// Get pointer to item in the stack.
#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
@@ -907,6 +908,9 @@ call_def_function(
current_sctx = ufunc->uf_script_ctx;
current_sctx.sc_version = SCRIPT_VERSION_VIM9;
+ // Do turn errors into exceptions.
+ suppress_errthrow = FALSE;
+
// Decide where to start execution, handles optional arguments.
init_instr_idx(ufunc, argc, &ectx);
@@ -1884,7 +1888,8 @@ call_def_function(
// push the next item from the list
if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
goto failed;
- if (++idxtv->vval.v_number >= list->lv_len)
+ ++idxtv->vval.v_number;
+ if (list == NULL || idxtv->vval.v_number >= list->lv_len)
// past the end of the list, jump to "endfor"
ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
else if (list->lv_first == &range_list_item)
@@ -2713,6 +2718,9 @@ failed_early:
vim_free(ectx.ec_stack.ga_data);
vim_free(ectx.ec_trystack.ga_data);
+ // Not sure if this is necessary.
+ suppress_errthrow = save_suppress_errthrow;
+
if (ret != OK && called_emsg == called_emsg_before)
semsg(_(e_unknown_error_while_executing_str),
printable_func_name(ufunc));