Patch 9.0.1501
Problem:    Crash with nested :try and :throw in catch block.
Solution:   Jump to :endtry before returning from function. (closes #12245)
Files:      src/vim9execute.c, src/testdir/test_vim9_script.vim


*** ../vim-9.0.1500/src/vim9execute.c   2023-04-18 21:04:49.431579535 +0100
--- src/vim9execute.c   2023-04-30 18:48:57.563368716 +0100
***************
*** 3053,3058 ****
--- 3053,3068 ----
                goto theend;
            did_throw = TRUE;
            *msg_list = NULL;
+ 
+           // This exception was not caught (yet).
+           garray_T    *trystack = &ectx->ec_trystack;
+           if (trystack->ga_len > 0)
+           {
+               trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
+                                                       + trystack->ga_len - 1;
+               if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
+                   trycmd->tcd_caught = FALSE;
+           }
        }
  
        if (unlikely(did_throw))
***************
*** 3066,3072 ****
            while (index > 0)
            {
                trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
!               if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
                    break;
                // In the catch and finally block of this try we have to go up
                // one level.
--- 3076,3086 ----
            while (index > 0)
            {
                trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
!               // 1. after :try and before :catch - jump to first :catch
!               // 2. in :catch block - jump to :finally
!               // 3. in :catch block and no finally - jump to :endtry
!               if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
!                               || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
                    break;
                // In the catch and finally block of this try we have to go up
                // one level.
***************
*** 3077,3096 ****
            {
                if (trycmd->tcd_in_catch)
                {
!                   // exception inside ":catch", jump to ":finally" once
!                   ectx->ec_iidx = trycmd->tcd_finally_idx;
!                   trycmd->tcd_finally_idx = 0;
                }
                else
                    // jump to first ":catch"
                    ectx->ec_iidx = trycmd->tcd_catch_idx;
!               trycmd->tcd_in_catch = TRUE;
                did_throw = FALSE;  // don't come back here until :endtry
                trycmd->tcd_did_throw = TRUE;
            }
            else
            {
!               // Not inside try or need to return from current functions.
                // Push a dummy return value.
                if (GA_GROW_FAILS(&ectx->ec_stack, 1))
                    goto theend;
--- 3091,3121 ----
            {
                if (trycmd->tcd_in_catch)
                {
!                   if (trycmd->tcd_finally_idx > 0)
!                   {
!                       // exception inside ":catch", jump to ":finally" once
!                       ectx->ec_iidx = trycmd->tcd_finally_idx;
!                       trycmd->tcd_finally_idx = 0;
!                   }
!                   else
!                   {
!                       // exception inside ":catch" or ":finally", jump to
!                       // ":endtry"
!                       ectx->ec_iidx = trycmd->tcd_endtry_idx;
!                   }
                }
                else
+               {
                    // jump to first ":catch"
                    ectx->ec_iidx = trycmd->tcd_catch_idx;
!                   trycmd->tcd_in_catch = TRUE;
!               }
                did_throw = FALSE;  // don't come back here until :endtry
                trycmd->tcd_did_throw = TRUE;
            }
            else
            {
!               // Not inside try or need to return from current function.
                // Push a dummy return value.
                if (GA_GROW_FAILS(&ectx->ec_stack, 1))
                    goto theend;
***************
*** 4652,4658 ****
  
                    if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
                    {
!                       // throwing an exception while using "silent!" causes
                        // the function to abort but not display an error.
                        tv = STACK_TV_BOT(-1);
                        clear_tv(tv);
--- 4677,4683 ----
  
                    if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
                    {
!                       // Throwing an exception while using "silent!" causes
                        // the function to abort but not display an error.
                        tv = STACK_TV_BOT(-1);
                        clear_tv(tv);
*** ../vim-9.0.1500/src/testdir/test_vim9_script.vim    2023-01-28 
19:38:45.785687265 +0000
--- src/testdir/test_vim9_script.vim    2023-04-30 17:45:59.927234001 +0100
***************
*** 812,817 ****
--- 812,852 ----
    v9.CheckDefAndScriptSuccess(lines)
  enddef
  
+ def Test_throw_in_nested_try()
+   var lines =<< trim END
+       vim9script
+ 
+       def Try(F: func(): void)
+         try
+           F()
+         catch
+         endtry
+       enddef
+ 
+       class X
+         def F()
+           try
+             throw 'Foobar'
+           catch
+             throw v:exception
+           endtry
+         enddef
+       endclass
+ 
+       def Test_TryMethod()
+         var x = X.new()
+         Try(() => x.F())
+       enddef
+ 
+ 
+       try
+         Test_TryMethod()
+       catch
+       endtry
+   END
+   v9.CheckScriptSuccess(lines)
+ enddef
+ 
  def Test_try_var_decl()
    var lines =<< trim END
        vim9script
*** ../vim-9.0.1500/src/version.c       2023-04-30 15:29:52.553140524 +0100
--- src/version.c       2023-04-30 17:46:07.243234583 +0100
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     1501,
  /**/

-- 
            |

Ceci n'est pas une pipe.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20230430175117.E966B1C03D0%40moolenaar.net.

Raspunde prin e-mail lui