Skip to content

Commit ee36e9a

Browse files
committed
Fix try/catch catches more than it should #1859
Rename the FORK_OPT opcode to TRY_BEGIN, add a TRY_END opcode, and wrap errors when raising through a TRY_END so that they will not be caught by the matching TRY_BEGIN. Now a `try exp catch handler` expression generates code like: TRY_BEGIN handler <exp> TRY_END JUMP past_handler handler: <handler> past_handler: ... On backtrack through TRY_BEGIN it just backtracks. If anything past the whole thing raises when <exp> produced a value, then the TRY_END will catch the error, wrap it in another, and backtrack. The TRY_BEGIN will see a wrapped error and then it will unwrap and re-raise the error. If <exp> raises, then TRY_BEGIN will catch the error and jump to the handler, but the TRY_BEGIN will not stack_save() in that case, so on raise/backtrack the TRY_BEGIN will not execute again (nor will the TRY_END).
1 parent 292502c commit ee36e9a

File tree

7 files changed

+436
-367
lines changed

7 files changed

+436
-367
lines changed

src/compile.c

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,43 +1057,40 @@ block gen_cond(block cond, block iftrue, block iffalse) {
10571057
BLOCK(gen_op_simple(POP), iffalse)));
10581058
}
10591059

1060-
block gen_try_handler(block handler) {
1061-
// Quite a pain just to hide jq's internal errors.
1062-
return gen_cond(// `if type=="object" and .__jq
1063-
gen_and(gen_call("_equal",
1064-
BLOCK(gen_lambda(gen_const(jv_string("object"))),
1065-
gen_lambda(gen_call("type", gen_noop())))),
1066-
BLOCK(gen_subexp(gen_const(jv_string("__jq"))),
1067-
gen_noop(),
1068-
gen_op_simple(INDEX))),
1069-
// `then error`
1070-
gen_call("error", gen_noop()),
1071-
// `else HANDLER end`
1072-
handler);
1073-
}
1074-
10751060
block gen_try(block exp, block handler) {
10761061
/*
1077-
* Produce something like:
1078-
* FORK_OPT <address of handler>
1062+
* Produce:
1063+
*
1064+
* TRY_BEGIN handler
10791065
* <exp>
1080-
* JUMP <end of handler>
1081-
* <handler>
1066+
* TRY_END
1067+
* JUMP past_handler
1068+
* handler: <handler>
1069+
* past_handler:
10821070
*
1083-
* If this is not an internal try/catch, then catch and re-raise
1084-
* internal errors to prevent them from leaking.
1071+
* If <exp> backtracks then TRY_BEGIN will backtrack.
10851072
*
1086-
* The handler will only execute if we backtrack to the FORK_OPT with
1087-
* an error (exception). If <exp> produces no value then FORK_OPT
1088-
* will backtrack (propagate the `empty`, as it were. If <exp>
1089-
* produces a value then we'll execute whatever bytecode follows this
1090-
* sequence.
1073+
* If <exp> produces a value then we'll execute whatever bytecode follows
1074+
* this sequence. If that code raises an exception, then TRY_END will wrap
1075+
* and re-raise that exception, and TRY_BEGIN will unwrap and re-raise the
1076+
* exception (see jq_next()).
1077+
*
1078+
* If <exp> raises then the TRY_BEGIN will see a non-wrapped exception and
1079+
* will jump to the handler (note the TRY_END will not execute in this case),
1080+
* and if the handler produces any values, then we'll execute whatever
1081+
* bytecode follows this sequence. Note that TRY_END will not execute in
1082+
* this case, so if the handler raises an exception, or code past the handler
1083+
* raises an exception, then that exception won't be wrapped and re-raised,
1084+
* and the TRY_BEGIN will not catch it because it does not stack_save() when
1085+
* it branches to the handler.
10911086
*/
1092-
if (!handler.first && !handler.last)
1093-
// A hack to deal with `.` as the handler; we could use a real NOOP here
1094-
handler = BLOCK(gen_op_simple(DUP), gen_op_simple(POP), handler);
1095-
exp = BLOCK(exp, gen_op_target(JUMP, handler));
1096-
return BLOCK(gen_op_target(FORK_OPT, exp), exp, handler);
1087+
1088+
if (block_is_noop(handler))
1089+
handler = BLOCK(gen_op_simple(DUP), gen_op_simple(POP));
1090+
1091+
block jump = gen_op_target(JUMP, handler);
1092+
return BLOCK(gen_op_target(TRY_BEGIN, jump), exp, gen_op_simple(TRY_END),
1093+
jump, handler);
10971094
}
10981095

10991096
block gen_label(const char *label, block exp) {

src/compile.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ block gen_destructure(block var, block matcher, block body);
6060
block gen_destructure_alt(block matcher);
6161

6262
block gen_cond(block cond, block iftrue, block iffalse);
63-
block gen_try_handler(block handler);
6463
block gen_try(block exp, block handler);
6564
block gen_label(const char *label, block exp);
6665

src/execute.c

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ jv jq_next(jq_state *jq) {
441441

442442
int raising;
443443
int backtracking = !jq->initial_execution;
444+
444445
jq->initial_execution = 0;
445446
assert(jv_get_kind(jq->error) == JV_KIND_NULL);
446447
assert(jq->parent == 0 || jq->start_limit >= jq->stk.limit);
@@ -910,15 +911,66 @@ jv jq_next(jq_state *jq) {
910911
break;
911912
}
912913

913-
case FORK_OPT:
914+
case TRY_BEGIN:
915+
stack_save(jq, pc - 1, stack_get_pos(jq));
916+
pc++; // skip handler offset this time
917+
break;
918+
919+
case TRY_END:
920+
stack_save(jq, pc - 1, stack_get_pos(jq));
921+
break;
922+
923+
case ON_BACKTRACK(TRY_BEGIN): {
924+
if (!raising) {
925+
/*
926+
* `try EXP ...` -- EXP backtracked (e.g., EXP was `empty`), so we
927+
* backtrack more:
928+
*/
929+
jv_free(stack_pop(jq));
930+
goto do_backtrack;
931+
}
932+
933+
/*
934+
* Else `(try EXP ... ) | EXP2` raised an error.
935+
*
936+
* If the error was wrapped in another error, then that means EXP2 raised
937+
* the error. We unwrap it and re-raise it as it wasn't raised by EXP.
938+
*
939+
* See commentary in gen_try().
940+
*/
941+
jv e = jv_invalid_get_msg(jv_copy(jq->error));
942+
if (!jv_is_valid(e) && jv_invalid_has_msg(jv_copy(e))) {
943+
set_error(jq, e);
944+
goto do_backtrack;
945+
}
946+
jv_free(e);
947+
948+
/*
949+
* Else we caught an error containing a non-error value, so we jump to
950+
* the handler.
951+
*
952+
* See commentary in gen_try().
953+
*/
954+
uint16_t offset = *pc++;
955+
jv_free(stack_pop(jq)); // free the input
956+
stack_push(jq, jv_invalid_get_msg(jq->error)); // push the error's message
957+
jq->error = jv_null();
958+
pc += offset;
959+
break;
960+
}
961+
case ON_BACKTRACK(TRY_END):
962+
// Wrap the error so the matching TRY_BEGIN doesn't catch it
963+
if (raising)
964+
set_error(jq, jv_invalid_with_msg(jv_copy(jq->error)));
965+
goto do_backtrack;
966+
914967
case DESTRUCTURE_ALT:
915968
case FORK: {
916969
stack_save(jq, pc - 1, stack_get_pos(jq));
917970
pc++; // skip offset this time
918971
break;
919972
}
920973

921-
case ON_BACKTRACK(FORK_OPT):
922974
case ON_BACKTRACK(DESTRUCTURE_ALT): {
923975
if (jv_is_valid(jq->error)) {
924976
// `try EXP ...` backtracked here (no value, `empty`), so we backtrack more

src/opcode_list.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ OP(STORE_GLOBAL, GLOBAL, 0, 0)
1111
OP(INDEX, NONE, 2, 1)
1212
OP(INDEX_OPT, NONE, 2, 1)
1313
OP(EACH, NONE, 1, 1)
14-
OP(EACH_OPT, NONE, 1, 1)
14+
OP(EACH_OPT, NONE, 1, 1)
1515
OP(FORK, BRANCH, 0, 0)
16-
OP(FORK_OPT, BRANCH, 0, 0)
16+
OP(TRY_BEGIN, BRANCH, 0, 0)
17+
OP(TRY_END, NONE, 0, 0)
1718
OP(JUMP, BRANCH, 0, 0)
1819
OP(JUMP_F,BRANCH, 1, 0)
1920
OP(BACKTRACK, NONE, 0, 0)

0 commit comments

Comments
 (0)