diff options
author | Yegappan Lakshmanan <yegappan@yahoo.com> | 2022-04-09 11:09:07 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-04-09 11:09:07 +0100 |
commit | 7c7e19cf50d76568e2637ad66b095044a41c6a82 (patch) | |
tree | 39534337197e3f8f2d713910050a977877effe33 | |
parent | 5e1792270a072a96157e5d5e1d6a97414e26d0bf (diff) | |
download | vim-git-7c7e19cf50d76568e2637ad66b095044a41c6a82.tar.gz |
patch 8.2.4716: memory allocation failure not tested when defining a functionv8.2.4716
Problem: Memory allocation failure not tested when defining a function.
Solution: Add a test. (Yegappan Lakshmanan, closes #10127)
-rw-r--r-- | src/alloc.c | 14 | ||||
-rw-r--r-- | src/alloc.h | 1 | ||||
-rw-r--r-- | src/proto/alloc.pro | 1 | ||||
-rw-r--r-- | src/testdir/test_user_func.vim | 30 | ||||
-rw-r--r-- | src/testdir/test_vim9_func.vim | 16 | ||||
-rw-r--r-- | src/userfunc.c | 2 | ||||
-rw-r--r-- | src/version.c | 2 |
7 files changed, 65 insertions, 1 deletions
diff --git a/src/alloc.c b/src/alloc.c index 9547d6719..5218d0046 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -719,6 +719,20 @@ ga_grow(garray_T *gap, int n) 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) { diff --git a/src/alloc.h b/src/alloc.h index 58ae25c14..651e01f24 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -44,5 +44,6 @@ typedef enum { aid_newwin_wvars, aid_newtabpage_tvars, aid_blob_alloc, + aid_get_func, aid_last } alloc_id_T; diff --git a/src/proto/alloc.pro b/src/proto/alloc.pro index 18281adaa..b605f28a5 100644 --- a/src/proto/alloc.pro +++ b/src/proto/alloc.pro @@ -19,6 +19,7 @@ int ga_copy_strings(garray_T *from, garray_T *to); 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); diff --git a/src/testdir/test_user_func.vim b/src/testdir/test_user_func.vim index 6eb8a0f33..f6e25e3b5 100644 --- a/src/testdir/test_user_func.vim +++ b/src/testdir/test_user_func.vim @@ -499,4 +499,34 @@ func Test_func_range() 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 diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim index 06168e477..9287ed5f3 100644 --- a/src/testdir/test_vim9_func.vim +++ b/src/testdir/test_vim9_func.vim @@ -4071,6 +4071,22 @@ def Test_go_beyond_end_of_cmd() 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() diff --git a/src/userfunc.c b/src/userfunc.c index c80a49051..7ad338856 100644 --- a/src/userfunc.c +++ b/src/userfunc.c @@ -1092,7 +1092,7 @@ get_function_body( } // Add the line to the function. - if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL) + if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL) goto theend; if (heredoc_concat_len > 0) diff --git a/src/version.c b/src/version.c index 5ffcdeb54..f8f15ad9f 100644 --- a/src/version.c +++ b/src/version.c @@ -747,6 +747,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 4716, +/**/ 4715, /**/ 4714, |