From 0a3a2132d43be3a376214f75157d4d2b1340311e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 4 May 2023 21:00:46 +0800 Subject: vim-patch:8.2.1953: Vim9: extra "unknown" error after other error Problem: Vim9: extra "unknown" error after other error. Solution: Restore did_emsg count after EXEC instruction. (closes vim/vim#7254) Improve error message from assert_fails() https://github.com/vim/vim/commit/631e8f93458b102091d54c502f489c03e454d4da Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/nvim/testing.c') diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 867a8ffd3a..27d5334fc5 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -142,7 +142,7 @@ static void ga_concat_shorten_esc(garray_T *gap, const char *str) } /// Fill "gap" with information about an assert error. -static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char *exp_str, +static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, const char *exp_str, typval_T *exp_tv_arg, typval_T *got_tv_arg, assert_type_T atype) { char *tofree; @@ -220,7 +220,13 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char *exp_str ga_concat_shorten_esc(gap, tofree); xfree(tofree); } else { + if (atype != ASSERT_INRANGE) { + ga_concat(gap, "'"); + } ga_concat_shorten_esc(gap, exp_str); + if (atype != ASSERT_INRANGE) { + ga_concat(gap, "'"); + } } if (atype != ASSERT_NOTEQUAL) { @@ -534,6 +540,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } else if (argvars[1].v_type != VAR_UNKNOWN) { char buf[NUMBUFLEN]; const char *expected; + const char *expected_str = NULL; bool error_found = false; int error_found_index = 1; char *actual = emsg_assert_fails_msg == NULL ? "[unknown]" : emsg_assert_fails_msg; @@ -551,12 +558,14 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) expected = tv_get_string_buf_chk(tv, buf); if (!pattern_match(expected, actual, false)) { error_found = true; + expected_str = expected; } else if (tv_list_len(list) == 2) { tv = TV_LIST_ITEM_TV(tv_list_last(list)); actual = get_vim_var_str(VV_ERRMSG); expected = tv_get_string_buf_chk(tv, buf); if (!pattern_match(expected, actual, false)) { error_found = true; + expected_str = expected; } } } else { @@ -600,7 +609,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) actual_tv.v_type = VAR_STRING; actual_tv.vval.v_string = actual; } - fill_assert_error(&ga, &argvars[2], NULL, + fill_assert_error(&ga, &argvars[2], expected_str, &argvars[error_found_index], &actual_tv, ASSERT_OTHER); ga_concat(&ga, ": "); assert_append_cmd_or_arg(&ga, argvars, cmd); -- cgit From 17c8e39f8803daa6f0e6106ce1c087240cef4771 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 4 May 2023 21:49:48 +0800 Subject: vim-patch:9.0.0213: using freed memory with error in assert argument Problem: Using freed memory with error in assert argument. Solution: Make a copy of the error. https://github.com/vim/vim/commit/249e1b903a9c0460d618f6dcc59aeb8c03b24b20 Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/nvim/testing.c') diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 27d5334fc5..25149f1b38 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -510,6 +510,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) const int save_trylevel = trylevel; const int called_emsg_before = called_emsg; const char *wrong_arg_msg = NULL; + char *tofree = NULL; if (tv_check_for_string_or_number_arg(argvars, 0) == FAIL || tv_check_for_opt_string_or_list_arg(argvars, 1) == FAIL @@ -560,8 +561,9 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) error_found = true; expected_str = expected; } else if (tv_list_len(list) == 2) { + // make a copy, an error in pattern_match() may free it + tofree = actual = xstrdup(get_vim_var_str(VV_ERRMSG)); tv = TV_LIST_ITEM_TV(tv_list_last(list)); - actual = get_vim_var_str(VV_ERRMSG); expected = tv_get_string_buf_chk(tv, buf); if (!pattern_match(expected, actual, false)) { error_found = true; @@ -632,6 +634,7 @@ theend: msg_reset_scroll(); lines_left = Rows; XFREE_CLEAR(emsg_assert_fails_msg); + xfree(tofree); set_vim_var_string(VV_ERRMSG, NULL, 0); if (wrong_arg_msg != NULL) { emsg(_(wrong_arg_msg)); -- cgit From 5fb6b3431a52932dcc50bcc930f8b2c9b71a1bb0 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:36:47 +0800 Subject: vim-patch:9.0.0404: crash when passing invalid arguments to assert_fails() Problem: Crash when passing invalid arguments to assert_fails(). Solution: Check for NULL string. https://github.com/vim/vim/commit/1540d334a04d874c2aa9d26b82dbbcd4bc5a78de Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/nvim/testing.c') diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 25149f1b38..92747822b4 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -531,6 +531,11 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) no_wait_return++; do_cmdline_cmd(cmd); + + // reset here for any errors reported below + trylevel = save_trylevel; + suppress_errthrow = false; + if (called_emsg == called_emsg_before) { prepare_assert_error(&ga); ga_concat(&ga, "command did not fail: "); @@ -557,6 +562,9 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } const typval_T *tv = TV_LIST_ITEM_TV(tv_list_first(list)); expected = tv_get_string_buf_chk(tv, buf); + if (expected == NULL) { + goto theend; + } if (!pattern_match(expected, actual, false)) { error_found = true; expected_str = expected; @@ -565,6 +573,9 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) tofree = actual = xstrdup(get_vim_var_str(VV_ERRMSG)); tv = TV_LIST_ITEM_TV(tv_list_last(list)); expected = tv_get_string_buf_chk(tv, buf); + if (expected == NULL) { + goto theend; + } if (!pattern_match(expected, actual, false)) { error_found = true; expected_str = expected; -- cgit From 49a2bb91170f49101fa67b0cf4cbfec5885edcf4 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:53:39 +0800 Subject: vim-patch:9.0.0846: using assert_fails() may cause hit-enter prompt Problem: Using assert_fails() may cause hit-enter prompt. Solution: Set no_wait_return. (closes vim/vim#11522) https://github.com/vim/vim/commit/f220643c260d55d21a841a3c4032daadc41bc50b Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/testing.c') diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 92747822b4..d9c376dd6a 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -522,14 +522,13 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) return; } - const char *const cmd = tv_get_string_chk(&argvars[0]); - // trylevel must be zero for a ":throw" command to be considered failed trylevel = 0; suppress_errthrow = true; in_assert_fails = true; no_wait_return++; + const char *const cmd = tv_get_string_chk(&argvars[0]); do_cmdline_cmd(cmd); // reset here for any errors reported below -- cgit From c11417b3d74f02568e37ea3370a7c24141d4f18a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:59:43 +0800 Subject: vim-patch:9.0.1507: assert message is confusing with boolean result Problem: Assert message is confusing with boolean result. assert_inrange() replaces message instead of adding it. Solution: Don't put quotes around expected boolean value. Append message for assert_inrange(). (closes vim/vim#12342, closes vim/vim#12341) https://github.com/vim/vim/commit/53f5e51628b56ef9171671cd6e9970374036a084 Move assert_type_T to testing.c and remove ASSERT_INRANGE. --- src/nvim/testing.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'src/nvim/testing.c') diff --git a/src/nvim/testing.c b/src/nvim/testing.c index d9c376dd6a..430d6713ff 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -30,6 +30,16 @@ #include "nvim/types.h" #include "nvim/vim.h" +/// Type of assert_* check being performed +typedef enum { + ASSERT_EQUAL, + ASSERT_NOTEQUAL, + ASSERT_MATCH, + ASSERT_NOTMATCH, + ASSERT_FAILS, + ASSERT_OTHER, +} assert_type_T; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "testing.c.generated.h" #endif @@ -220,11 +230,11 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, const char *e ga_concat_shorten_esc(gap, tofree); xfree(tofree); } else { - if (atype != ASSERT_INRANGE) { + if (atype == ASSERT_FAILS) { ga_concat(gap, "'"); } ga_concat_shorten_esc(gap, exp_str); - if (atype != ASSERT_INRANGE) { + if (atype == ASSERT_FAILS) { ga_concat(gap, "'"); } } @@ -622,7 +632,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) actual_tv.vval.v_string = actual; } fill_assert_error(&ga, &argvars[2], expected_str, - &argvars[error_found_index], &actual_tv, ASSERT_OTHER); + &argvars[error_found_index], &actual_tv, ASSERT_FAILS); ga_concat(&ga, ": "); assert_append_cmd_or_arg(&ga, argvars, cmd); assert_error(&ga); @@ -672,16 +682,9 @@ static int assert_inrange(typval_T *argvars) if (factual < flower || factual > fupper) { garray_T ga; prepare_assert_error(&ga); - if (argvars[3].v_type != VAR_UNKNOWN) { - char *const tofree = encode_tv2string(&argvars[3], NULL); - ga_concat(&ga, tofree); - xfree(tofree); - } else { - char msg[80]; - vim_snprintf(msg, sizeof(msg), "Expected range %g - %g, but got %g", - flower, fupper, factual); - ga_concat(&ga, msg); - } + char expected_str[200]; + vim_snprintf(expected_str, sizeof(expected_str), "range %g - %g,", flower, fupper); + fill_assert_error(&ga, &argvars[3], expected_str, NULL, &argvars[2], ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); return 1; @@ -697,13 +700,11 @@ static int assert_inrange(typval_T *argvars) if (actual < lower || actual > upper) { garray_T ga; prepare_assert_error(&ga); - - char msg[55]; - vim_snprintf(msg, sizeof(msg), + char expected_str[200]; + vim_snprintf(expected_str, sizeof(expected_str), "range %" PRIdVARNUMBER " - %" PRIdVARNUMBER ",", lower, upper); // -V576 - fill_assert_error(&ga, &argvars[3], msg, NULL, &argvars[2], - ASSERT_INRANGE); + fill_assert_error(&ga, &argvars[3], expected_str, NULL, &argvars[2], ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); return 1; -- cgit