Skip to content

Commit 2051af1

Browse files
ychinchrisbra
authored andcommitted
patch 9.1.0999: Vim9: leaking finished exception
Problem: leaking finished exception (after v9.1.0984) Solution: use finish_exception to clean up caught exceptions (Yee Cheng Chin) In Vimscript, v:exception/throwpoint/stacktrace are supposed to reflect the currently caught exception, and be popped after the exception is finished (via endtry, finally, or a thrown exception inside catch). Vim9script does not handle this properly, and leaks them instead. This is clearly visible when launching GVim with menu enabled. A caught exception inside the s:BMShow() in menu.vim would show up when querying `v:stacktrace` even though the exception was already caught and handled. To fix this, just use the same functionality as Vimscript by calling `finish_exception` to properly restore the states. Note that this assumes `current_exception` is always the same as `caught_stack` which believe should be the case. Added tests for this. Also fix up test_stacktrace to properly test the stack restore behavior where we have nested exceptions in catch blocks and to also test the vim9script functionality properly. - Also, remove its dependency on explicitly checking a line number in runtest.vim which is a very fragile way to write tests as any minor change in runtest.vim (shared among all tests) would require changing test_stacktrace.vim. We don't actually need such granularity in the test. closes: #16413 Signed-off-by: Yee Cheng Chin <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent df4a7d7 commit 2051af1

File tree

6 files changed

+91
-15
lines changed

6 files changed

+91
-15
lines changed

src/ex_eval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ catch_exception(except_T *excp)
718718
/*
719719
* Remove an exception from the caught stack.
720720
*/
721-
static void
721+
void
722722
finish_exception(except_T *excp)
723723
{
724724
if (excp != caught_stack)

src/proto/ex_eval.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ char *get_exception_string(void *value, except_type_T type, char_u *cmdname, int
1111
int throw_exception(void *value, except_type_T type, char_u *cmdname);
1212
void discard_current_exception(void);
1313
void catch_exception(except_T *excp);
14+
void finish_exception(except_T *excp);
1415
void exception_state_save(exception_state_T *estate);
1516
void exception_state_restore(exception_state_T *estate);
1617
void exception_state_clear(void);

src/testdir/test_stacktrace.vim

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func Filepath(name)
1010
endfunc
1111

1212
func AssertStacktrace(expect, actual)
13-
call assert_equal(#{lnum: 617, filepath: Filepath('runtest.vim')}, a:actual[0])
13+
call assert_equal(Filepath('runtest.vim'), a:actual[0]['filepath'])
1414
call assert_equal(a:expect, a:actual[-len(a:expect):])
1515
endfunc
1616

@@ -97,16 +97,28 @@ func Test_vstacktrace()
9797
call Xfunc1()
9898
catch
9999
let stacktrace = v:stacktrace
100+
try
101+
call Xfunc1()
102+
catch
103+
let stacktrace_inner = v:stacktrace
104+
endtry
105+
let stacktrace_after = v:stacktrace " should be restored by the exception stack to the previous one
100106
endtry
101107
call assert_equal([], v:stacktrace)
102108
call AssertStacktrace([
103109
\ #{funcref: funcref('Test_vstacktrace'), lnum: 97, filepath: s:thisfile},
104110
\ #{funcref: funcref('Xfunc1'), lnum: 5, filepath: Filepath('Xscript1')},
105111
\ #{funcref: funcref('Xfunc2'), lnum: 4, filepath: Filepath('Xscript2')},
106112
\ ], stacktrace)
113+
call AssertStacktrace([
114+
\ #{funcref: funcref('Test_vstacktrace'), lnum: 101, filepath: s:thisfile},
115+
\ #{funcref: funcref('Xfunc1'), lnum: 5, filepath: Filepath('Xscript1')},
116+
\ #{funcref: funcref('Xfunc2'), lnum: 4, filepath: Filepath('Xscript2')},
117+
\ ], stacktrace_inner)
118+
call assert_equal(stacktrace, stacktrace_after)
107119
endfunc
108120

109-
func Test_zzz_stacktrace_vim9()
121+
func Test_stacktrace_vim9()
110122
let lines =<< trim [SCRIPT]
111123
var stacktrace = getstacktrace()
112124
assert_notequal([], stacktrace)
@@ -122,11 +134,9 @@ func Test_zzz_stacktrace_vim9()
122134
assert_true(has_key(d, 'lnum'))
123135
endfor
124136
endtry
137+
call assert_equal([], v:stacktrace)
125138
[SCRIPT]
126139
call v9.CheckDefSuccess(lines)
127-
" FIXME: v:stacktrace is not cleared after the exception handling, and this
128-
" test has to be run as the last one because of this.
129-
" call assert_equal([], v:stacktrace)
130140
endfunc
131141

132142
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_vim9_script.vim

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,47 @@ def Test_try_catch_throw()
945945
endif
946946
END
947947
v9.CheckDefAndScriptSuccess(lines)
948+
949+
# test that the v:exception stacks are correctly restored
950+
try
951+
try
952+
throw 101
953+
catch
954+
assert_equal('101', v:exception)
955+
try
956+
catch
957+
finally
958+
assert_equal('101', v:exception) # finally shouldn't clear if it doesn't own it
959+
endtry
960+
assert_equal('101', v:exception)
961+
throw 102 # Re-throw inside catch block
962+
endtry
963+
catch
964+
assert_equal('102', v:exception)
965+
try
966+
throw 103 # throw inside nested exception stack
967+
catch
968+
assert_equal('103', v:exception)
969+
endtry
970+
assert_equal('102', v:exception) # restored stack
971+
finally
972+
assert_equal('', v:exception) # finally should clear if it owns the exception
973+
endtry
974+
try
975+
try
976+
throw 104
977+
catch
978+
try
979+
exec 'nonexistent_cmd' # normal exception inside nested exception stack
980+
catch
981+
assert_match('E492:', v:exception)
982+
endtry
983+
eval [][0] # normal exception inside catch block
984+
endtry
985+
catch
986+
assert_match('E684:', v:exception)
987+
endtry
988+
assert_equal('', v:exception) # All exceptions properly popped
948989
enddef
949990

950991
def Test_unreachable_after()
@@ -1417,11 +1458,23 @@ def Test_throw_line_number()
14171458
eval 2 + 2
14181459
throw 'exception'
14191460
enddef
1461+
def Func2()
1462+
eval 1 + 1
1463+
eval 2 + 2
1464+
eval 3 + 3
1465+
throw 'exception'
1466+
enddef
14201467
try
14211468
Func()
14221469
catch /exception/
1470+
try
1471+
Func2()
1472+
catch /exception/
1473+
assert_match('line 4', v:throwpoint)
1474+
endtry
14231475
assert_match('line 3', v:throwpoint)
14241476
endtry
1477+
assert_match('', v:throwpoint)
14251478
enddef
14261479

14271480

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
999,
707709
/**/
708710
998,
709711
/**/

src/vim9execute.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,7 +3281,15 @@ exec_instructions(ectx_T *ectx)
32813281
trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
32823282
+ trystack->ga_len - 1;
32833283
if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3284-
trycmd->tcd_caught = FALSE;
3284+
{
3285+
if (trycmd->tcd_caught)
3286+
{
3287+
// Inside a "catch" we need to first discard the caught
3288+
// exception.
3289+
finish_exception(caught_stack);
3290+
trycmd->tcd_caught = FALSE;
3291+
}
3292+
}
32853293
}
32863294
}
32873295

@@ -4972,6 +4980,12 @@ exec_instructions(ectx_T *ectx)
49724980
// Reset the index to avoid a return statement jumps here
49734981
// again.
49744982
trycmd->tcd_finally_idx = 0;
4983+
if (trycmd->tcd_caught)
4984+
{
4985+
// discard the exception
4986+
finish_exception(caught_stack);
4987+
trycmd->tcd_caught = FALSE;
4988+
}
49754989
break;
49764990
}
49774991

@@ -4986,12 +5000,10 @@ exec_instructions(ectx_T *ectx)
49865000
trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
49875001
if (trycmd->tcd_did_throw)
49885002
did_throw = TRUE;
4989-
if (trycmd->tcd_caught && current_exception != NULL)
5003+
if (trycmd->tcd_caught)
49905004
{
49915005
// discard the exception
4992-
if (caught_stack == current_exception)
4993-
caught_stack = caught_stack->caught;
4994-
discard_current_exception();
5006+
finish_exception(caught_stack);
49955007
}
49965008

49975009
if (trycmd->tcd_return)
@@ -5040,12 +5052,10 @@ exec_instructions(ectx_T *ectx)
50405052
{
50415053
trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
50425054
+ trystack->ga_len - 1;
5043-
if (trycmd->tcd_caught && current_exception != NULL)
5055+
if (trycmd->tcd_caught)
50445056
{
50455057
// discard the exception
5046-
if (caught_stack == current_exception)
5047-
caught_stack = caught_stack->caught;
5048-
discard_current_exception();
5058+
finish_exception(caught_stack);
50495059
trycmd->tcd_caught = FALSE;
50505060
}
50515061
}

0 commit comments

Comments
 (0)