]> granicus.if.org Git - vim/commitdiff
patch 8.1.1591: on error garbage collection may free memory in use v8.1.1591
authorBram Moolenaar <Bram@vim.org>
Tue, 25 Jun 2019 04:28:02 +0000 (06:28 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 25 Jun 2019 04:28:02 +0000 (06:28 +0200)
Problem:    On error garbage collection may free memory in use.
Solution:   Reset may_garbage_collect when evaluating expression mapping.
            Add tests. (Ozaki Kiichi, closes #4579)

src/ex_cmds2.c
src/getchar.c
src/testdir/test_mapping.vim
src/testdir/test_timers.vim
src/testdir/test_vimscript.vim
src/version.c

index 8871f5dbfa3d5e6a902fcf5e52799849f4ea1ef5..036a2a73523e163d61529885dd4d1420c61f2a6d 100644 (file)
@@ -367,8 +367,8 @@ check_due_timer(void)
            int save_vgetc_busy = vgetc_busy;
            int save_did_emsg = did_emsg;
            int save_called_emsg = called_emsg;
-           int save_must_redraw = must_redraw;
-           int save_trylevel = trylevel;
+           int save_must_redraw = must_redraw;
+           int save_trylevel = trylevel;
            int save_did_throw = did_throw;
            int save_ex_pressedreturn = get_pressedreturn();
            int save_may_garbage_collect = may_garbage_collect;
index 80e98ec60e25b9aa449495943a61a01d9433df5e..f6734a79a611ea5acefff5eb3ab99701ecdb5f66 100644 (file)
@@ -2540,13 +2540,18 @@ vgetorpeek(int advance)
                         */
                        if (mp->m_expr)
                        {
-                           int         save_vgetc_busy = vgetc_busy;
+                           int save_vgetc_busy = vgetc_busy;
+                           int save_may_garbage_collect = may_garbage_collect;
 
                            vgetc_busy = 0;
+                           may_garbage_collect = FALSE;
+
                            save_m_keys = vim_strsave(mp->m_keys);
                            save_m_str = vim_strsave(mp->m_str);
                            s = eval_map_expr(save_m_str, NUL);
+
                            vgetc_busy = save_vgetc_busy;
+                           may_garbage_collect = save_may_garbage_collect;
                        }
                        else
 #endif
index a361b2fdc8dec463733cb9efa69c5551533a978f..7f90b0385cec01a19780c344395f06691cc7574b 100644 (file)
@@ -397,3 +397,39 @@ func Test_motionforce_omap()
   delfunc Select
   delfunc GetCommand
 endfunc
+
+func Test_error_in_map_expr()
+  if !has('terminal') || (has('win32') && has('gui_running'))
+    throw 'Skipped: cannot run Vim in a terminal window'
+  endif
+
+  let lines =<< trim [CODE]
+  func Func()
+    " fail to create list
+    let x = [
+  endfunc
+  nmap <expr> ! Func()
+  set updatetime=50
+  [CODE]
+  call writefile(lines, 'Xtest.vim')
+
+  let buf = term_start(GetVimCommandClean() .. ' -S Xtest.vim', {'term_rows': 8})
+  let job = term_getjob(buf)
+  call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
+
+  " GC must not run during map-expr processing, which can make Vim crash.
+  call term_sendkeys(buf, '!')
+  call term_wait(buf, 100)
+  call term_sendkeys(buf, "\<CR>")
+  call term_wait(buf, 100)
+  call assert_equal('run', job_status(job))
+
+  call term_sendkeys(buf, ":qall!\<CR>")
+  call WaitFor({-> job_status(job) ==# 'dead'})
+  if has('unix')
+    call assert_equal('', job_info(job).termsig)
+  endif
+
+  call delete('Xtest.vim')
+  exe buf .. 'bwipe!'
+endfunc
index 906613497f97b598168968d9d795b9178baaf383..de13bcbe89d21ae5fff85a1149bac8f2bc89f1ab 100644 (file)
@@ -333,4 +333,39 @@ func Test_nocatch_garbage_collect()
   delfunc FeedChar
 endfunc
 
+func Test_error_in_timer_callback()
+  if !has('terminal') || (has('win32') && has('gui_running'))
+    throw 'Skipped: cannot run Vim in a terminal window'
+  endif
+
+  let lines =<< trim [CODE]
+  func Func(timer)
+    " fail to create list
+    let x = [
+  endfunc
+  set updatetime=50
+  call timer_start(1, 'Func')
+  [CODE]
+  call writefile(lines, 'Xtest.vim')
+
+  let buf = term_start(GetVimCommandClean() .. ' -S Xtest.vim', {'term_rows': 8})
+  let job = term_getjob(buf)
+  call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
+
+  " GC must not run during timer callback, which can make Vim crash.
+  call term_wait(buf, 100)
+  call term_sendkeys(buf, "\<CR>")
+  call term_wait(buf, 100)
+  call assert_equal('run', job_status(job))
+
+  call term_sendkeys(buf, ":qall!\<CR>")
+  call WaitFor({-> job_status(job) ==# 'dead'})
+  if has('unix')
+    call assert_equal('', job_info(job).termsig)
+  endif
+
+  call delete('Xtest.vim')
+  exe buf .. 'bwipe!'
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index 328d19fa5a4973630dbf2fac450842f881a3b91c..bf3e052ec950b77cb7d31b06ef7d0c6e43808cde 100644 (file)
@@ -1665,7 +1665,7 @@ func Test_refcount()
     delfunc DictFunc
 endfunc
 
-func! Test_funccall_garbage_collect()
+func Test_funccall_garbage_collect()
     func Func(x, ...)
         call add(a:x, a:000)
     endfunc
index 5db753ca4c8e19e0c28329d8b25d43f6ef5d23d5..c21c12c6d7f470f3db5f7622c07fe04b6a1ccb46 100644 (file)
@@ -777,6 +777,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1591,
 /**/
     1590,
 /**/