Patch 8.2.1704
Problem:    Vim9: crash in for loop when autoload script has an error.
Solution:   Reset suppress_errthrow.  Check for NULL list. (closes #6967)
Files:      src/vim9execute.c, src/testdir/test_vim9_script.vim


*** ../vim-8.2.1703/src/vim9execute.c   2020-09-16 22:29:49.441083728 +0200
--- src/vim9execute.c   2020-09-17 21:27:51.388176406 +0200
***************
*** 761,766 ****
--- 761,767 ----
      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,912 ****
--- 908,916 ----
      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,1890 ****
                    // 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)
                        // 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)
--- 1888,1895 ----
                    // push the next item from the list
                    if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
                        goto failed;
!                   ++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,2718 ****
--- 2718,2726 ----
      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));
*** ../vim-8.2.1703/src/testdir/test_vim9_script.vim    2020-09-16 
22:29:49.441083728 +0200
--- src/testdir/test_vim9_script.vim    2020-09-17 21:26:06.392520891 +0200
***************
*** 3353,3358 ****
--- 3353,3397 ----
    &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.
*** ../vim-8.2.1703/src/version.c       2020-09-17 19:59:20.520623502 +0200
--- src/version.c       2020-09-17 21:08:02.564162548 +0200
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     1704,
  /**/

-- 
We apologise again for the fault in the subtitles.  Those responsible for
sacking the people who have just been sacked have been sacked.
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202009171929.08HJTTrU2478419%40masaka.moolenaar.net.

Raspunde prin e-mail lui