Skip to content
Myr

Fix reduce to preserve its input across init backtracking (#3633)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`reduce f as $x (init; update)` evaluated `f` against a stack slot that forkpoints created while evaluating `init` restore. Both the `DUPN` opening the reduction loop and the `LOADVN` retrieving the result pop that slot destructively, nulling it out, so an `init` that backtracks (e.g. `0, 0`) made `f` see `null` and fail with "Cannot iterate over null". Duplicate the input before the `FORK` instead of inside the loop, and drop the leftover copy with `POP` once the loop is exhausted. The slot left live at the `FORK` is then a block pushed after `init` completed, which no earlier forkpoint can reach, so nulling it is harmless. This is the same approach `gen_foreach` has used since c70ae1a. `DUPN` had no other user, so remove the opcode.

authored by itchyny · committed by GitHub · parent 4beed4e · Browse files · 4 files+10 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+10 added−11 removed

  • tests:+7 added−1 removed
  • other:+3 added−10 removed

4 files+10 added−11 removed

modified:src/compile.c+3 added-2 removed
830 unmodified lines
831}832833block gen_reduce(block source, block matcher, block init, block body) {834 block res_var = gen_op_var_fresh(STOREV, "reduce");835 block loop = BLOCK(gen_op_simple(DUPN),836 source,835 block loop = BLOCK(source,836 bind_alternation_matchers(matcher,837 BLOCK(gen_op_bound(LOADVN, res_var),838 body,839 gen_op_bound(STOREV, res_var))),840 gen_op_simple(BACKTRACK));841 return BLOCK(gen_op_simple(DUP),842 init,843 res_var,844 gen_op_simple(DUP),845 gen_op_target(FORK, loop),846 loop,847 gen_op_simple(POP),848 gen_op_bound(LOADVN, res_var));849}850851block gen_foreach(block source, block matcher, block init, block update, block extract) {
547 unmodified lines
modified:src/execute.c-7 removed
426 unmodified lines
427 stack_push(jq, v);428 break;429 }430431 case DUPN: {432 jv v = stack_popn(jq);433 stack_push(jq, jv_copy(v));434 stack_push(jq, v);435 break;436 }437431 case DUP2: {432 jv keep = stack_pop(jq);433 jv v = stack_pop(jq);434 stack_push(jq, jv_copy(v));
912 unmodified lines
modified:src/opcode_list.h-1 removed
1OP(LOADK, CONSTANT, 1, 1)2OP(DUP, NONE, 1, 2)3OP(DUPN, NONE, 1, 2)3OP(DUP2, NONE, 2, 3)4OP(PUSHK_UNDER, CONSTANT, 1, 2)5OP(POP, NONE, 1, 0)6OP(LOADV, VARIABLE, 1, 1)
44 unmodified lines
modified:tests/jq.test+7 added-1 removed
2569 unmodified lines
2570try ["OK", setpath([[1]]; 1)] catch ["KO", .]2571[]2572["KO","Cannot update field at array index of array"]25732574# regression test for #32272574# regression test for #3227 (reduce)2575[] | reduce .[] as $x (0, 1; .)2576null257702578125792580# regression test for #3227 (foreach)2581foreach .[] as $x (0, 1; . + $x)2582[1, 2]2583125843
94 unmodified lines