Skip to content

Commit 4b11483

Browse files
zsxOldes
authored andcommitted
ATRONIX: Fix: Restore Saved_State/Halt_State when a longjmp happens
Fixes: Oldes/Rebol-issues#2190 Illustrated by: ``` attempt [; this sets Saved_State catch/quit [ ;this calls Try_Block_Halt and sets Halt_State print x ; this causes an error, and calls ;"longjmp(*State_State)", which invalidates ; Halt_State above. ] ] load %./ ;Just tries to fill up the C stack and messes up "Halt_State". halt ; Jumps to the invalid "Halt_State", and crashes ``` or ``` catch/quit [ ;sets Halt_State attempt [ ;sets Saved_State quit ; jumps to Halt_State, and invalidates "Saved_State" ] ] print x ; Causes a jump to the invalid "Saved_State" ``` (cherry picked from commit 91b4fbd)
1 parent d2b8cd5 commit 4b11483

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/core/c-do.c

+6
Original file line numberDiff line numberDiff line change
@@ -1082,9 +1082,12 @@ x*/ static REBINT Do_Args_Light(REBVAL *func, REBVAL *path, REBSER *block, REBCN
10821082
{
10831083
REBOL_STATE state;
10841084
REBVAL *tos;
1085+
jmp_buf *Last_Halt_State = Halt_State;
10851086

10861087
PUSH_STATE(state, Saved_State);
10871088
if (SET_JUMP(state)) {
1089+
/* Halt_State might become invalid, restore the one above */
1090+
Halt_State = Last_Halt_State;
10881091
POP_STATE(state, Saved_State);
10891092
Catch_Error(DS_NEXT); // Stores error value here
10901093
return TRUE;
@@ -1662,6 +1665,7 @@ x*/ static REBINT Do_Args_Light(REBVAL *func, REBVAL *path, REBSER *block, REBCN
16621665
{
16631666
REBOL_STATE state;
16641667
REBVAL *val;
1668+
jmp_buf *Last_Saved_State = Saved_State;
16651669
// static D = 0;
16661670
// int depth = D++;
16671671

@@ -1670,6 +1674,8 @@ x*/ static REBINT Do_Args_Light(REBVAL *func, REBVAL *path, REBSER *block, REBCN
16701674
PUSH_STATE(state, Halt_State);
16711675
if (SET_JUMP(state)) {
16721676
// Debug_Fmt("Throw Halt %d", depth);
1677+
/* Saved_State might become invalid, restore the one above */
1678+
Saved_State = Last_Saved_State;
16731679
POP_STATE(state, Halt_State);
16741680
Catch_Error(DS_NEXT); // Stores error value here
16751681
return TRUE;

src/tests/units/crash-test.r3

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Rebol [
88

99
~~~start-file~~~ "Crash tests"
1010

11-
===start-group=== "Series crashes"
11+
===start-group=== "Crashing issues"
1212

1313
--test-- "DH keys generation"
1414
;@@ situation fixed in: https://github.com/zsx/r3/commit/cc625bebcb6038b9282876954f929c9d80048d2b
@@ -28,6 +28,11 @@ Rebol [
2828
a: func [/b] [1]
2929
--assert error? try [a/b/%] ;- no crash, just error!
3030

31+
--test-- "issue-2190"
32+
;@@ https://github.com/Oldes/Rebol-issues/issues/2190
33+
catch/quit [ attempt [ quit ] ]
34+
--assert error? try [print x] ;- no crash, just error!
35+
3136
===end-group===
3237

3338
~~~end-file~~~

0 commit comments

Comments
 (0)