changeset 22381:6fe9536694ff v8.2.1739

patch 8.2.1739: Vim9: crash when compiling a manually defined function Commit: https://github.com/vim/vim/commit/9c4f55204fdf8909f4e3515a32a542044bf9f943 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Sep 25 21:47:28 2020 +0200 patch 8.2.1739: Vim9: crash when compiling a manually defined function Problem: Vim9: crash when compiling a manually defined function. (Antony Scriven) Solution: Check that the script ID is positive. (closes #7012)
author Bram Moolenaar <Bram@vim.org>
date Fri, 25 Sep 2020 22:00:04 +0200
parents 4b2dec9c0c29
children dcfdfcb1f6c9
files src/testdir/test_vim9_script.vim src/version.c src/vim9compile.c
diffstat 3 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -2755,7 +2755,7 @@ def Test_vim9_autoload_error()
     exe 'set rtp^=' .. getcwd() .. '/Xruntime'
     call crash#func()
     call writefile(['ok'], 'Xdidit')
-    qall
+    qall!
   END
   writefile(lines, 'Xscript')
   RunVim([], [], '-S Xscript')
@@ -2817,7 +2817,7 @@ enddef
 def Test_invalid_sid()
   assert_fails('func <SNR>1234_func', 'E123:')
 
-  if RunVim([], ['wq Xdidit'], '+"func <SNR>1_func"')
+  if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
     assert_equal([], readfile('Xdidit'))
   endif
   delete('Xdidit')
@@ -2831,6 +2831,27 @@ def Test_unset_any_variable()
   CheckDefAndScriptSuccess(lines)
 enddef
 
+def Test_define_func_at_command_line()
+  # run in a separate Vim instance to avoid the script context
+  let lines =<< trim END
+    func CheckAndQuit()
+      call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
+      call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
+    endfunc
+  END
+  writefile([''], 'Xdidcmd')
+  writefile(lines, 'XcallFunc')
+  let buf = RunVimInTerminal('-S XcallFunc', #{rows: 6})
+  # define Afunc() on the command line
+  term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
+  term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
+  WaitForAssert({-> assert_equal(['errors: []'], readfile('Xdidcmd'))})
+
+  call StopVimInTerminal(buf)
+  delete('XcallFunc')
+  delete('Xdidcmd')
+enddef
+
 " Keep this last, it messes up highlighting.
 def Test_substitute_cmd()
   new
--- 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 */
 /**/
+    1739,
+/**/
     1738,
 /**/
     1737,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -277,9 +277,12 @@ script_is_vim9()
 lookup_script(char_u *name, size_t len, int vim9script)
 {
     int		    cc;
-    hashtab_T	    *ht = &SCRIPT_VARS(current_sctx.sc_sid);
+    hashtab_T	    *ht;
     dictitem_T	    *di;
 
+    if (current_sctx.sc_sid <= 0)
+	return FAIL;
+    ht = &SCRIPT_VARS(current_sctx.sc_sid);
     if (vim9script && !script_is_vim9())
 	return FAIL;
     cc = name[len];