Skip to content

fix(executor): trap set -u in arithmetic array subscripts on write (#639) - #654

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/issue639-nounset-subscript
Oct 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/issue639-nounset-subscript

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

fix(executor): trap set -u in arithmetic array subscripts on the write path (#639)

What

set -u; a[k]=v silently stored with status 0. GNU evaluates every
subscript through evalexp → expr_streval, whose unbound block aborts on
both read and write (expr.c:1190-1216). The write-path subscript
evaluator was missing the nounset prologue its arithmetic
command/expansion siblings have.

Fix

  • eval_indexed_subscript_expression: the same nounset prologue as
    eval_arithmetic_command_value / eval_arithmetic_expansion_value
    (scan + single message + flag). Assoc keys bypass arithmetic entirely,
    so declare -A A; A[k]=v stays silent by construction. No memo changes:
    the subscript memo frames are per-word-expansion, so a store without
    nounset cannot mask a later trap and vice versa (probed both toggle
    directions).
  • All five report_indexed_subscript_error sites stay silent when the flag
    is set (message already printed once).
  • New raise_nounset_subscript_abort (consumes the flag so a caught abort
    never poisons later commands): abandon with the context-owned fatal
    status at the element-write arm, both compound rewrite sites, the
    assign-default path, both unset arms, and both wait-var write paths.
  • The trap is FORCE_EOF, not DISCARD: a distinct nounset_force_eof cell
    rides the shared abandon vehicle, and the ast abandon point recomputes
    the context status there (127 at -c, 1 in script) so no intermediate
    exit-code write can downgrade it. Every boundary that clears the pending
    flag clears the cell alongside (subshell/comsub/external/function
    resets); subshell containment verified (( set -u; a[k]=v ); echo after
    prints after, rc 0).
  • Compound m=([k]=v) needed one more hop: its failure returns through
    apply_shell_assignment, whose generic failure maps to
    ExpansionFailure(1) — the nounset cell now diverts to
    Err(ExitCode(fatal)) first (mirrors the POSIX arm directly below it).

Verification

  • New 25-case GNU differential battery: 10/25 on master → 25/25.
    Covers write/append/compound/declare/unset-existing traps, read
    controls, assoc negatives, both toggle directions, function/subshell/
    eval/redirect/default/-v shapes. (Local GNU is 5.3.20, not the WSL
    5.3.0 oracle — every rule cited from vendored expr.c, and the one
    known version-sensitive number, the for-header status, is recorded.)
  • -c matrix: write/append/compound/declare/unset-existing all abandon
    with rc 127; script mode rc 1; unset on a missing array stays silent
    with rc 0 (GNU evaluates subscripts only when the array exists —
    probed).
  • New tests/issue639_nounset_subscript.rs (11 tests, -c byte asserts
    incl. the 127 status).
  • Focused suites green (lib 584, parity, 371, 638, 631, 614,
    compound-quote, 536); full serial failure-set gate vs bc258ad:
    target sets identical (248=248 ok, 26=26 failed, NEW=0), test-level 1
    NEW / 1 DISAPPEARED both in the known-nondeterministic part_080
    coproc family (both pass in isolation on both trees); niubash product
    A/B failure sets identical (49=49) apart from the known load-flaky 50x
    timing-ratio test (passes and fails back-to-back on the same tree).

Out of scope (tracked separately)

GNU owners: expr.c:1190-1216 expr_streval, error.c:447 err_unboundvar,
arrayfunc.c:1356 array_expand_index, shell.c:1471 vs :1477
(FORCE_EOF vs DISCARD catch), subst.c:11169 (assignment-error jump).

)

set -u; a[k]=v silently stored with status 0. GNU evaluates every
subscript through evalexp -> expr_streval, whose unbound block aborts on
both read and write (expr.c:1190-1216). The write-path subscript
evaluator was missing the nounset prologue its arithmetic
command/expansion siblings have; added it, silenced the five downstream
operand-expected diagnostics on the flag, and routed the abort with the
context-owned fatal status at every write consumer (element write,
compound rewrite x2, assign-default, unset x2, wait-var x2).

The trap is FORCE_EOF, not DISCARD: a distinct nounset_force_eof cell
rides the shared abandon vehicle, consumed at the ast abandon point
which recomputes the context status there (127 at -c, 1 in script), with
every pending-clearing boundary clearing the cell alongside. Compound
m=([k]=v) diverts to Err(ExitCode(fatal)) at the apply_shell_assignment
failure mapping. No memo changes (frames are per-word-expansion).

Verification: new 25-case GNU differential 10/25 -> 25/25; -c matrix all
127 with abandon (script mode 1; unset-on-missing stays silent per GNU);
new tests/issue639_nounset_subscript.rs (11 tests); lib 584 + focused
suites green; full serial failure-set gate vs bc258ad identical (NEW=0;
1/1 part_080 coproc flake passing in isolation both sides); niubash A/B
49=49 apart from the known load-flaky timing test (passes+fails
back-to-back on one tree).
@caomengxuan666
caomengxuan666 merged commit ea9c18e into master Oct 16, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/issue639-nounset-subscript branch October 16, 2026 05:59
caomengxuan666 added a commit that referenced this pull request Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant