]> granicus.if.org Git - vim/commitdiff
patch 8.2.4668: buffer allocation failures insufficiently tested v8.2.4668
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 2 Apr 2022 20:46:19 +0000 (21:46 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 2 Apr 2022 20:46:19 +0000 (21:46 +0100)
Problem:    Buffer allocation failures insufficiently tested.
Solution:   Add tests for memory allocation failures. (Yegappan Lakshmanan,
            closes #10064)

src/alloc.h
src/buffer.c
src/popupwin.c
src/testdir/test_buffer.vim
src/testdir/test_swap.vim
src/version.c
src/window.c

index 9260018994635edee1e4870444e26c69f9f14044..35c00b6083c97ede9b001003b39f29c56955f0e0 100644 (file)
@@ -40,5 +40,6 @@ typedef enum {
     aid_sign_getplaced_list,
     aid_insert_sign,
     aid_sign_getinfo,
+    aid_buflistnew_bvars,
     aid_last
 } alloc_id_T;
index 93da5dcc32e6d3e4d88555e1f2f28f6ed9dc909f..c8e63c34e10401ac07825a36c6f972779f15a195 100644 (file)
@@ -2093,7 +2093,7 @@ buflist_new(
        }
 #ifdef FEAT_EVAL
        // init b: variables
-       buf->b_vars = dict_alloc();
+       buf->b_vars = dict_alloc_id(aid_buflistnew_bvars);
        if (buf->b_vars == NULL)
        {
            vim_free(ffname);
index 883372c47fdf6e39e1ea8c963ff2f26bd79f957b..c3251eae13d2e48e4e1370b2e261952de3f70785 100644 (file)
@@ -1997,7 +1997,10 @@ popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
        new_buffer = TRUE;
        buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
        if (buf == NULL)
+       {
+           win_free_popup(wp);
            return NULL;
+       }
        ml_open(buf);
 
        win_init_popup_win(wp, buf);
index 0d871383fa7ae19a4ae3d1c154f4c6b6730a7091..fd96c618c03fea8357a1c3794dc14db93e925938 100644 (file)
@@ -430,4 +430,74 @@ func Test_buffer_maxmem()
   set maxmem& maxmemtot&
 endfunc
 
+" Test for a allocation failure when adding a new buffer
+func Test_buflist_alloc_failure()
+  %bw!
+
+  edit Xfile1
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('edit Xfile2', 'E342:')
+
+  " test for bufadd()
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('call bufadd("Xbuffer")', 'E342:')
+
+  " test for setting the arglist
+  edit Xfile2
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('next Xfile3', 'E342:')
+
+  " test for setting the alternate buffer name when writing a file
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('write Xother', 'E342:')
+  call delete('Xother')
+
+  " test for creating a buffer using bufnr()
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails("call bufnr('Xnewbuf', v:true)", 'E342:')
+
+  " test for renaming buffer using :file
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('file Xnewfile', 'E342:')
+
+  " test for creating a buffer for a popup window
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('call popup_create("mypop", {})', 'E342:')
+
+  if has('terminal')
+    " test for creating a buffer for a terminal window
+    call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+    call assert_fails('call term_start(&shell)', 'E342:')
+    %bw!
+  endif
+
+  " test for loading a new buffer after wiping out all the buffers
+  edit Xfile4
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('%bw!', 'E342:')
+
+  " test for :checktime loading the buffer
+  call writefile(['one'], 'Xfile5')
+  if has('unix')
+    edit Xfile5
+    " sleep for some time to make sure the timestamp is different
+    sleep 200m
+    call writefile(['two'], 'Xfile5')
+    set autoread
+    call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+    call assert_fails('checktime', 'E342:')
+    set autoread&
+    bw!
+  endif
+
+  " test for :vimgrep loading a dummy buffer
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('vimgrep two Xfile5', 'E342:')
+  call delete('Xfile5')
+
+  " test for quickfix command loading a buffer
+  call test_alloc_fail(GetAllocId('buflistnew_bvars'), 0, 0)
+  call assert_fails('cexpr "Xfile6:10:Line10"', 'E342:')
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index 143a9af19c4b39ae97269299f7dfb4b0bfc882af..a46494003387662cccfcd7ea0b317dc9a8216574 100644 (file)
@@ -233,7 +233,6 @@ func Test_swap_recover()
     autocmd SwapExists * let v:swapchoice = 'r'
   augroup END
 
-
   call mkdir('Xswap')
   let $Xswap = 'foo'  " Check for issue #4369.
   set dir=Xswap//
index 67427039c23ccfd98653225998b56f1524715c1f..311b878e6a9442cd3f1aeb8c67cabe1a390b82e3 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4668,
 /**/
     4667,
 /**/
index 66dd099bf6f6929aee999016902ec609ef3ed76b..78ce4d235db68b8503ce35b65706de5521565793 100644 (file)
@@ -5256,10 +5256,13 @@ win_unlisted(win_T *wp)
     void
 win_free_popup(win_T *win)
 {
-    if (bt_popup(win->w_buffer))
-       win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE);
-    else
-       close_buffer(win, win->w_buffer, 0, FALSE, FALSE);
+    if (win->w_buffer != NULL)
+    {
+       if (bt_popup(win->w_buffer))
+           win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE);
+       else
+           close_buffer(win, win->w_buffer, 0, FALSE, FALSE);
+    }
 # if defined(FEAT_TIMERS)
     if (win->w_popup_timer != NULL)
        stop_timer(win->w_popup_timer);