To: vim_dev@googlegroups.com Subject: Patch 8.2.4716 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.4716 Problem: Memory allocation failure not tested when defining a function. Solution: Add a test. (Yegappan Lakshmanan, closes #10127) Files: src/alloc.c, src/alloc.h, src/proto/alloc.pro, src/userfunc.c, src/testdir/test_user_func.vim, src/testdir/test_vim9_func.vim *** ../vim-8.2.4715/src/alloc.c 2022-04-03 21:57:22.513883898 +0100 --- src/alloc.c 2022-04-09 10:59:49.596563459 +0100 *************** *** 719,724 **** --- 719,738 ---- return OK; } + /* + * Same as ga_grow() but uses an allocation id for testing. + */ + int + ga_grow_id(garray_T *gap, int n, alloc_id_T id UNUSED) + { + #ifdef FEAT_EVAL + if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T))) + return FAIL; + #endif + + return ga_grow(gap, n); + } + int ga_grow_inner(garray_T *gap, int n) { *** ../vim-8.2.4715/src/alloc.h 2022-04-03 11:22:33.524172298 +0100 --- src/alloc.h 2022-04-09 10:59:49.596563459 +0100 *************** *** 44,48 **** --- 44,49 ---- aid_newwin_wvars, aid_newtabpage_tvars, aid_blob_alloc, + aid_get_func, aid_last } alloc_id_T; *** ../vim-8.2.4715/src/proto/alloc.pro 2022-01-08 15:39:35.410385289 +0000 --- src/proto/alloc.pro 2022-04-09 10:59:49.596563459 +0100 *************** *** 19,24 **** --- 19,25 ---- void ga_init(garray_T *gap); void ga_init2(garray_T *gap, size_t itemsize, int growsize); int ga_grow(garray_T *gap, int n); + int ga_grow_id(garray_T *gap, int n, alloc_id_T id UNUSED); int ga_grow_inner(garray_T *gap, int n); char_u *ga_concat_strings(garray_T *gap, char *sep); int ga_copy_string(garray_T *gap, char_u *p); *** ../vim-8.2.4715/src/userfunc.c 2022-04-04 14:58:02.166539812 +0100 --- src/userfunc.c 2022-04-09 10:59:49.596563459 +0100 *************** *** 1092,1098 **** } // Add the line to the function. ! if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL) goto theend; if (heredoc_concat_len > 0) --- 1092,1098 ---- } // Add the line to the function. ! if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL) goto theend; if (heredoc_concat_len > 0) *** ../vim-8.2.4715/src/testdir/test_user_func.vim 2021-12-26 20:20:29.093631230 +0000 --- src/testdir/test_user_func.vim 2022-04-09 10:59:49.596563459 +0100 *************** *** 499,502 **** --- 499,532 ---- bwipe! endfunc + " Test for memory allocation failure when defining a new function + func Test_funcdef_alloc_failure() + new + let lines =<< trim END + func Xtestfunc() + return 321 + endfunc + END + call setline(1, lines) + call test_alloc_fail(GetAllocId('get_func'), 0, 0) + call assert_fails('source', 'E342:') + call assert_false(exists('*Xtestfunc')) + call assert_fails('delfunc Xtestfunc', 'E117:') + %d _ + let lines =<< trim END + def g:Xvim9func(): number + return 456 + enddef + END + call setline(1, lines) + call test_alloc_fail(GetAllocId('get_func'), 0, 0) + call assert_fails('source', 'E342:') + call assert_false(exists('*Xvim9func')) + "call test_alloc_fail(GetAllocId('get_func'), 0, 0) + "call assert_fails('source', 'E342:') + "call assert_false(exists('*Xtestfunc')) + "call assert_fails('delfunc Xtestfunc', 'E117:') + bw! + endfunc + " vim: shiftwidth=2 sts=2 expandtab *** ../vim-8.2.4715/src/testdir/test_vim9_func.vim 2022-03-23 11:29:08.560730204 +0000 --- src/testdir/test_vim9_func.vim 2022-04-09 10:59:49.596563459 +0100 *************** *** 4071,4076 **** --- 4071,4092 ---- v9.CheckScriptFailure(lines, 'E476:') enddef + " Test for memory allocation failure when defining a new lambda + func Test_lambda_allocation_failure() + new + let lines =<< trim END + vim9script + g:Xlambda = (x): number => { + return x + 1 + } + END + call setline(1, lines) + call test_alloc_fail(GetAllocId('get_func'), 0, 0) + call assert_fails('source', 'E342:') + call assert_false(exists('g:Xlambda')) + bw! + endfunc + " The following messes up syntax highlight, keep near the end. if has('python3') def Test_python3_command() *** ../vim-8.2.4715/src/version.c 2022-04-08 19:55:36.216168883 +0100 --- src/version.c 2022-04-09 11:01:27.984931411 +0100 *************** *** 748,749 **** --- 748,751 ---- { /* Add new patch number below this line */ + /**/ + 4716, /**/ -- Mynd you, m00se bites Kan be pretty nasti ... "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///