Fix try/catch catches more than it should #1859

Close #1885, #2140, #2011, #2220, #2485, #2073

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).
This commit is contained in:
Nico Williams authored and GitHub committed 2023-07-24 13:20:21 +02:00
1 parent 98f709d0c1
commit 1cf6515c06
7 files changed
+468 -368

No files matched your search

+28 -31
View File
@@ -1034,43 +1034,40 @@ block gen_cond(block cond, block iftrue, block iffalse) {
BLOCK(gen_op_simple(POP), iffalse)));
}
block gen_try_handler(block handler) {
// Quite a pain just to hide jq's internal errors.
return gen_cond(// `if type=="object" and .__jq
gen_and(gen_call("_equal",
BLOCK(gen_lambda(gen_const(jv_string("object"))),
gen_lambda(gen_call("type", gen_noop())))),
BLOCK(gen_subexp(gen_const(jv_string("__jq"))),
gen_noop(),
gen_op_simple(INDEX))),
// `then error`
gen_call("error", gen_noop()),
// `else HANDLER end`
handler);
}
block gen_try(block exp, block handler) {
/*
* Produce something like:
* FORK_OPT <address of handler>
* Produce:
*
* TRY_BEGIN handler
* <exp>
* JUMP <end of handler>
* <handler>
* TRY_END
* JUMP past_handler
* handler: <handler>
* past_handler:
*
* If this is not an internal try/catch, then catch and re-raise
* internal errors to prevent them from leaking.
* If <exp> backtracks then TRY_BEGIN will backtrack.
*
* The handler will only execute if we backtrack to the FORK_OPT with
* an error (exception). If <exp> produces no value then FORK_OPT
* will backtrack (propagate the `empty`, as it were. If <exp>
* produces a value then we'll execute whatever bytecode follows this
* sequence.
* If <exp> produces a value then we'll execute whatever bytecode follows
* this sequence. If that code raises an exception, then TRY_END will wrap
* and re-raise that exception, and TRY_BEGIN will unwrap and re-raise the
* exception (see jq_next()).
*
* If <exp> raises then the TRY_BEGIN will see a non-wrapped exception and
* will jump to the handler (note the TRY_END will not execute in this case),
* and if the handler produces any values, then we'll execute whatever
* bytecode follows this sequence. Note that TRY_END will not execute in
* this case, so if the handler raises an exception, or code past the handler
* raises an exception, then that exception won't be wrapped and re-raised,
* and the TRY_BEGIN will not catch it because it does not stack_save() when
* it branches to the handler.
*/
if (!handler.first && !handler.last)
// A hack to deal with `.` as the handler; we could use a real NOOP here
handler = BLOCK(gen_op_simple(DUP), gen_op_simple(POP), handler);
exp = BLOCK(exp, gen_op_target(JUMP, handler));
return BLOCK(gen_op_target(FORK_OPT, exp), exp, handler);
if (block_is_noop(handler))
handler = BLOCK(gen_op_simple(DUP), gen_op_simple(POP));
block jump = gen_op_target(JUMP, handler);
return BLOCK(gen_op_target(TRY_BEGIN, jump), exp, gen_op_simple(TRY_END),
jump, handler);
}
block gen_label(const char *label, block exp) {