Skip to content

Commit d9d7789

Browse files
committed
patch 8.2.2503: Vim9: a caught error may leave something on the stack
Problem: Vim9: a caught error may leave something on the stack. Solution: Drop items from the stack if needed. (closes #7826)
1 parent ca753ec commit d9d7789

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/testdir/test_vim9_script.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,16 @@ def Test_try_catch_throw()
556556
n = 411
557557
endtry
558558
assert_equal(411, n)
559+
560+
var counter = 0
561+
for i in range(4)
562+
try
563+
eval [][0]
564+
catch
565+
endtry
566+
counter += 1
567+
endfor
568+
assert_equal(4, counter)
559569
enddef
560570

561571
def Test_cnext_works_in_catch()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2503,
753755
/**/
754756
2502,
755757
/**/

src/vim9execute.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
// Structure put on ec_trystack when ISN_TRY is encountered.
2626
typedef struct {
27-
int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
27+
int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28+
int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
2829
int tcd_catch_idx; // instruction of the first catch
2930
int tcd_finally_idx; // instruction of the finally block
3031
int tcd_caught; // catch block entered
@@ -2561,6 +2562,7 @@ call_def_function(
25612562
++ectx.ec_trystack.ga_len;
25622563
++trylevel;
25632564
trycmd->tcd_frame_idx = ectx.ec_frame_idx;
2565+
trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
25642566
trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
25652567
trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
25662568
trycmd->tcd_caught = FALSE;
@@ -2632,6 +2634,12 @@ call_def_function(
26322634

26332635
if (trycmd->tcd_return)
26342636
goto func_return;
2637+
2638+
while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2639+
{
2640+
--ectx.ec_stack.ga_len;
2641+
clear_tv(STACK_TV_BOT(0));
2642+
}
26352643
}
26362644
}
26372645
break;

0 commit comments

Comments
 (0)