From b6026337f25cc708e932b21a9e4e64a174a1d9da Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sat, 9 Oct 2021 01:25:11 +0100 Subject: vim-patch:8.2.3416: second error is reported while exception is being thrown Problem: Second error is reported while exception is being thrown. Solution: Do not check for trailing characters when already aborting. (closes vim/vim#8842) https://github.com/vim/vim/commit/36f691f5f1d0676f080cc97d697d742ed5cc8251 --- src/nvim/eval/userfunc.c | 2 +- src/nvim/testdir/test_trycatch.vim | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index eb5c6e503a..972bd81117 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -3018,7 +3018,7 @@ void ex_call(exarg_T *eap) } // When inside :try we need to check for following "| catch". - if (!failed || eap->cstack->cs_trylevel > 0) { + if (!aborting() && (!failed || eap->cstack->cs_trylevel > 0)) { // Check for trailing illegal characters and a following command. if (!ends_excmd(*arg)) { if (!failed) { diff --git a/src/nvim/testdir/test_trycatch.vim b/src/nvim/testdir/test_trycatch.vim index adc1745b39..04423fa988 100644 --- a/src/nvim/testdir/test_trycatch.vim +++ b/src/nvim/testdir/test_trycatch.vim @@ -1996,5 +1996,29 @@ func Test_reload_in_try_catch() call delete('Xreload') endfunc +" Test for using throw in a called function with following error {{{1 +func Test_user_command_throw_in_function_call() + let lines =<< trim END + function s:get_dict() abort + throw 'my_error' + endfunction + + try + call s:get_dict().foo() + catch /my_error/ + let caught = 'yes' + catch + let caught = 'no' + endtry + call assert_equal('yes', caught) + END + call writefile(lines, 'XtestThrow') + source XtestThrow + + call delete('XtestThrow') + unlet g:caught +endfunc + + " Modeline {{{1 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker -- cgit From 93c72d866b3a41c429dd9d278cda7059ebd4afba Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sat, 9 Oct 2021 01:34:18 +0100 Subject: vim-patch:8.2.3448: :endtry after function call that throws not found Problem: :endtry after function call that throws not found. Solution: Do check for following :endtry if an exception is being thrown. (closes vim/vim#8889) https://github.com/vim/vim/commit/1d34189ecb99fa76363c06e1aa815c1075675a1c Nvim obsoleted did_throw; check current_exception is not NULL instead. --- src/nvim/eval/userfunc.c | 7 ++++--- src/nvim/testdir/test_trycatch.vim | 28 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index 972bd81117..0fadc0d220 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -3017,11 +3017,12 @@ void ex_call(exarg_T *eap) } } - // When inside :try we need to check for following "| catch". - if (!aborting() && (!failed || eap->cstack->cs_trylevel > 0)) { + // When inside :try we need to check for following "| catch" or "| endtry". + // Not when there was an error, but do check if an exception was thrown. + if ((!aborting() || current_exception != NULL) && (!failed || eap->cstack->cs_trylevel > 0)) { // Check for trailing illegal characters and a following command. if (!ends_excmd(*arg)) { - if (!failed) { + if (!failed && !aborting()) { emsg_severe = true; emsg(_(e_trailing)); } diff --git a/src/nvim/testdir/test_trycatch.vim b/src/nvim/testdir/test_trycatch.vim index 04423fa988..e7b163c94b 100644 --- a/src/nvim/testdir/test_trycatch.vim +++ b/src/nvim/testdir/test_trycatch.vim @@ -2008,7 +2008,7 @@ func Test_user_command_throw_in_function_call() catch /my_error/ let caught = 'yes' catch - let caught = 'no' + let caught = v:exception endtry call assert_equal('yes', caught) END @@ -2019,6 +2019,32 @@ func Test_user_command_throw_in_function_call() unlet g:caught endfunc +" Test for using throw in a called function with following endtry {{{1 +func Test_user_command_function_call_with_endtry() + let lines =<< trim END + funct s:throw(msg) abort + throw a:msg + endfunc + func s:main() abort + try + try + throw 'err1' + catch + call s:throw('err2') | endtry + catch + let s:caught = 'yes' + endtry + endfunc + + call s:main() + call assert_equal('yes', s:caught) + END + call writefile(lines, 'XtestThrow') + source XtestThrow + + call delete('XtestThrow') +endfunc + " Modeline {{{1 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker -- cgit From a25c35d6e409398a8d8304210784e020c262aec5 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sat, 9 Oct 2021 02:13:49 +0100 Subject: vim-patch:8.2.3470: crash with error in :catch and also in :finally Problem: Crash with error in :catch and also in :finally. Solution: Only discard an exception if there is one. (closes vim/vim#8954) https://github.com/vim/vim/commit/a684a684096ecef3fbaee39c573b47423235d6b1 --- src/nvim/ex_eval.c | 2 +- src/nvim/testdir/test_trycatch.vim | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/ex_eval.c b/src/nvim/ex_eval.c index 3c7c635d98..d9c14f143e 100644 --- a/src/nvim/ex_eval.c +++ b/src/nvim/ex_eval.c @@ -1913,7 +1913,7 @@ int cleanup_conditionals(cstack_T *cstack, int searched_cond, int inclusive) default: if (cstack->cs_flags[idx] & CSF_FINALLY) { - if (cstack->cs_pending[idx] & CSTP_THROW) { + if ((cstack->cs_pending[idx] & CSTP_THROW) && cstack->cs_exception[idx] != NULL) { // Cancel the pending exception. This is in the // finally clause, so that the stack of the // caught exceptions is not involved. diff --git a/src/nvim/testdir/test_trycatch.vim b/src/nvim/testdir/test_trycatch.vim index e7b163c94b..f625f52906 100644 --- a/src/nvim/testdir/test_trycatch.vim +++ b/src/nvim/testdir/test_trycatch.vim @@ -2045,6 +2045,18 @@ func Test_user_command_function_call_with_endtry() call delete('XtestThrow') endfunc +func ThisWillFail() + try + if x | endif + catch + for l in [] + finally +endfunc + +func Test_error_in_catch_and_finally() + call assert_fails('call ThisWillFail()', ['E121:', 'E600:']) +endfunc + " Modeline {{{1 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker -- cgit From 64150517967fbe7fb111320c3dc1d16528aa547b Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sat, 9 Oct 2021 02:52:21 +0100 Subject: refactor(ex_eval): cherry-pick CSF flags changes from v8.2.3099 https://github.com/vim/vim/commit/4197828dc666f2d258594f7f9461534d23cc50e4 Cherry-pick the changes to existing flags values. Required for v8.2.3478. That patch mostly relates to Vim9script, but I'm careful not to mark it N/A in case the flags have some use outside of Vim9 in the future. Excludes CSF_FUNC_DEF (flag introduced in v8.2.1870 for Vim9's block scopes). --- src/nvim/ex_eval.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/ex_eval.h b/src/nvim/ex_eval.h index 15da4b2d60..07247ea7d5 100644 --- a/src/nvim/ex_eval.h +++ b/src/nvim/ex_eval.h @@ -14,9 +14,9 @@ #define CSF_TRY 0x0100 // is a ":try" #define CSF_FINALLY 0x0200 // ":finally" has been passed -#define CSF_THROWN 0x0400 // exception thrown to this try conditional -#define CSF_CAUGHT 0x0800 // exception caught by this try conditional -#define CSF_SILENT 0x1000 // "emsg_silent" reset by ":try" +#define CSF_THROWN 0x0800 // exception thrown to this try conditional +#define CSF_CAUGHT 0x1000 // exception caught by this try conditional +#define CSF_SILENT 0x2000 // "emsg_silent" reset by ":try" // Note that CSF_ELSE is only used when CSF_TRY and CSF_WHILE are unset // (an ":if"), and CSF_SILENT is only used when CSF_TRY is set. -- cgit From 5feb8cdbb4d1ac01288058112702279c476f5aea Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sat, 9 Oct 2021 02:17:45 +0100 Subject: vim-patch:8.2.3478: still crash with error in :catch and also in :finally Problem: Still crash with error in :catch and also in :finally. Solution: Only call finish_exception() once. (closes vim/vim#8954) https://github.com/vim/vim/commit/f67d3fb7363ebc9454f9bb582de3978609a4fd6b Exclude CSF_FUNC_DEF change (Vim9script). --- src/nvim/ex_eval.c | 3 ++- src/nvim/ex_eval.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/ex_eval.c b/src/nvim/ex_eval.c index d9c14f143e..b5456e8982 100644 --- a/src/nvim/ex_eval.c +++ b/src/nvim/ex_eval.c @@ -1934,8 +1934,9 @@ int cleanup_conditionals(cstack_T *cstack, int searched_cond, int inclusive) */ if (!(cstack->cs_flags[idx] & CSF_FINALLY)) { if ((cstack->cs_flags[idx] & CSF_ACTIVE) - && (cstack->cs_flags[idx] & CSF_CAUGHT)) { + && (cstack->cs_flags[idx] & CSF_CAUGHT) && !(cstack->cs_flags[idx] & CSF_FINISHED)) { finish_exception((except_T *)cstack->cs_exception[idx]); + cstack->cs_flags[idx] |= CSF_FINISHED; } // Stop at this try conditional - except the try block never // got active (because of an inactive surrounding conditional diff --git a/src/nvim/ex_eval.h b/src/nvim/ex_eval.h index 07247ea7d5..1cd0a47d7a 100644 --- a/src/nvim/ex_eval.h +++ b/src/nvim/ex_eval.h @@ -16,7 +16,8 @@ #define CSF_FINALLY 0x0200 // ":finally" has been passed #define CSF_THROWN 0x0800 // exception thrown to this try conditional #define CSF_CAUGHT 0x1000 // exception caught by this try conditional -#define CSF_SILENT 0x2000 // "emsg_silent" reset by ":try" +#define CSF_FINISHED 0x2000 // CSF_CAUGHT was handled by finish_exception() +#define CSF_SILENT 0x4000 // "emsg_silent" reset by ":try" // Note that CSF_ELSE is only used when CSF_TRY and CSF_WHILE are unset // (an ":if"), and CSF_SILENT is only used when CSF_TRY is set. -- cgit From 789558d365a3134a33cdf4f1f3cc5e77efcad687 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sat, 9 Oct 2021 02:20:07 +0100 Subject: vim-patch:8.2.3480: test does not fail without the fix for a crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Test does not fail without the fix for a crash. Solution: Write the bad code in a file and source it. (Dominique Pellé, closes vim/vim#8961) https://github.com/vim/vim/commit/949de97da32d4fff28c569387e2ba8b3e311e64d --- src/nvim/testdir/test_trycatch.vim | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/nvim/testdir/test_trycatch.vim b/src/nvim/testdir/test_trycatch.vim index f625f52906..34c402fc73 100644 --- a/src/nvim/testdir/test_trycatch.vim +++ b/src/nvim/testdir/test_trycatch.vim @@ -2046,17 +2046,26 @@ func Test_user_command_function_call_with_endtry() endfunc func ThisWillFail() - try - if x | endif - catch - for l in [] - finally + endfunc +" This was crashing prior to the fix in 8.2.3478. func Test_error_in_catch_and_finally() - call assert_fails('call ThisWillFail()', ['E121:', 'E600:']) -endfunc + let lines =<< trim END + try + echo x + catch + for l in [] + finally + END + call writefile(lines, 'XtestCatchAndFinally') + try + source XtestCatchAndFinally + catch /E600:/ + endtry + call delete('XtestCatchAndFinally') +endfunc " Modeline {{{1 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker -- cgit From fc954d0a61ca8952bdcd05f66fe07ae2a4ccb712 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 8 Oct 2021 21:05:38 +0100 Subject: vim-patch:8.2.3486: illegal memory access with invalid sequence of commands Problem: Illegal memory access with invalid sequence of commands. Solution: Do not call leave_block() when not in a try block. (closes vim/vim#8966) Reset did_emsg so that exception is shown as an error. https://github.com/vim/vim/commit/cce81e9673fe8d056e8eef310d9919620eccb2f2 Vim9script is N/A, which includes leave_block. --- src/nvim/ex_eval.c | 12 +++++++++--- src/nvim/testdir/test_trycatch.vim | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/ex_eval.c b/src/nvim/ex_eval.c index b5456e8982..f4aaab5c43 100644 --- a/src/nvim/ex_eval.c +++ b/src/nvim/ex_eval.c @@ -1083,7 +1083,7 @@ void ex_endwhile(exarg_T *eap) if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0) { eap->errmsg = err; } else { - fl = cstack->cs_flags[cstack->cs_idx]; + fl = cstack->cs_flags[cstack->cs_idx]; if (!(fl & csf)) { // If we are in a ":while" or ":for" but used the wrong endloop // command, do not rewind to the next enclosing ":for"/":while". @@ -1575,6 +1575,7 @@ void ex_endtry(exarg_T *eap) if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY)) { eap->errmsg = get_end_emsg(cstack); + // Find the matching ":try" and report what's missing. idx = cstack->cs_idx; do { @@ -1594,6 +1595,9 @@ void ex_endtry(exarg_T *eap) if (current_exception) { discard_current_exception(); } + + // report eap->errmsg, also when there already was an error + did_emsg = false; } else { idx = cstack->cs_idx; @@ -1664,8 +1668,10 @@ void ex_endtry(exarg_T *eap) */ (void)cleanup_conditionals(cstack, CSF_TRY | CSF_SILENT, TRUE); - --cstack->cs_idx; - --cstack->cs_trylevel; + if (cstack->cs_idx >= 0 && (cstack->cs_flags[cstack->cs_idx] & CSF_TRY)) { + cstack->cs_idx--; + } + cstack->cs_trylevel--; if (!skip) { report_resume_pending(pending, diff --git a/src/nvim/testdir/test_trycatch.vim b/src/nvim/testdir/test_trycatch.vim index 34c402fc73..205ed095ea 100644 --- a/src/nvim/testdir/test_trycatch.vim +++ b/src/nvim/testdir/test_trycatch.vim @@ -2067,5 +2067,26 @@ func Test_error_in_catch_and_finally() call delete('XtestCatchAndFinally') endfunc +" This was causing an illegal memory access +func Test_leave_block_in_endtry_not_called() + let lines =<< trim END + " vim9script + " try # + try " + for x in [] + if + endwhile + if + endtry + END + call writefile(lines, 'XtestEndtry') + try + source XtestEndtry + catch /E171:/ + endtry + + call delete('XtestEndtry') +endfunc + " Modeline {{{1 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker -- cgit From 76e6b81b23c59ee119d6cc34eed0ef580f15db07 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 8 Oct 2021 20:44:58 +0100 Subject: vim-patch:8.2.3487: illegal memory access if buffer name is very long Problem: Illegal memory access if buffer name is very long. Solution: Make sure not to go over the end of the buffer. https://github.com/vim/vim/commit/826bfe4bbd7594188e3d74d2539d9707b1c6a14b Adjust the test to use :noswapfile, as Nvim fails to create the swap file on Windows due to the file name's length (E303). We don't have this behaviour on Linux as we get "[Permission denied]" from readfile(), so there is no attempt to create the swap file. However, Vim doesn't try to create the swap file on Windows either for a different reason: MAXPATHL in Vim for Windows is only 1024 (compared to Nvim's 4096 on the Windows CI), so readfile() gives "Illegal file name" instead, thus not needing :noswapfile for both cases. --- src/nvim/screen.c | 10 +++++----- src/nvim/testdir/test_statusline.vim | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 296255ed8c..5a33ebfb37 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -5173,19 +5173,19 @@ static void win_redr_status(win_T *wp) *(p + len++) = ' '; } if (bt_help(wp->w_buffer)) { - STRCPY(p + len, _("[Help]")); + snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Help]")); len += (int)STRLEN(p + len); } if (wp->w_p_pvw) { - STRCPY(p + len, _("[Preview]")); + snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Preview]")); len += (int)STRLEN(p + len); } if (bufIsChanged(wp->w_buffer)) { - STRCPY(p + len, "[+]"); - len += 3; + snprintf((char *)p + len, MAXPATHL - len, "%s", "[+]"); + len += (int)STRLEN(p + len); } if (wp->w_buffer->b_p_ro) { - STRCPY(p + len, _("[RO]")); + snprintf((char *)p + len, MAXPATHL - len, "%s", _("[RO]")); // len += (int)STRLEN(p + len); // dead assignment } diff --git a/src/nvim/testdir/test_statusline.vim b/src/nvim/testdir/test_statusline.vim index fad13e3340..492d09c645 100644 --- a/src/nvim/testdir/test_statusline.vim +++ b/src/nvim/testdir/test_statusline.vim @@ -523,4 +523,16 @@ func Test_statusline_mbyte_fillchar() %bw! endfunc +" Used to write beyond allocated memory. This assumes MAXPATHL is 4096 bytes. +func Test_statusline_verylong_filename() + let fname = repeat('x', 4090) + " Nvim's swap file creation fails on Windows (E303) due to fname's length + " exe "new " .. fname + exe "noswapfile new " .. fname + set buftype=help + set previewwindow + redraw + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab -- cgit