Skip to content

WIP: fix #2558 pre-scan :? double evaluation of subscripted parameters - #2593

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2558-prescan-double-fetch
Nov 2, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2558-prescan-double-fetch

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2558 — do not close on merge; captain adjudicates the residual below.

Defect

The command-level :?/? error pre-scan fetched a SET subscripted
parameter a second time on the no-error path (its evaluating fetch
queued the subscript's side-effect writes; the real expansion fetched
again against the overlay). : ${a[i++]:?err} incremented i twice;
echo ${a[i++]:?err} returned a[1] instead of a[0]; sequential
commands could overshoot into a genuinely unset element and die.
GNU fetches exactly once: subst.c:9777 parameter_brace_expand →
arrayfunc.c array_value_internal (subscript evaluated inside the
single fetch); subst.c:8216-8230 expands the message word only on
the error path.

The previously prescribed skip-fix was implemented and refuted by
W12 (five absent-element shapes went silent); this PR is the
replacement route, chosen after in-tree route analysis (full text in
the cluster REPORT): expander-side adjudication needs a new fatal
channel consumed at many scattered points; cross-pass value handoff
has no sound key (the tree's three memo precedents are all
per-expansion-pass; a text key collides on the two-fragment pin).

Fix

Scan-boundary deferred-write isolation. The pre-scan's fetches are
&self, so on the no-error path their only env-state footprint is
the PENDING_SUBSCRIPT_WRITES queue. Each top-level scan entry now
marks the queue length and truncates back to the mark when the scan
reaches no error verdict; on an error verdict the queue is left
untouched (command aborts; the scan's evaluation is the only one;
state byte-identical to before). Whole-scan granularity preserves
intra-scan sequencing (sparse-array verdicts depend on later
fragments seeing earlier fragments' queued writes); nested windows
compose. Bonus: the speculative admission probes
(parameter_expansion_error_pending, comsub shortcut probe) are now
actually side-effect-free for the write channel, as documented.

Verification (build slot + scrubbed cargo, exact stems)

  • tests/issue2558_prescan_double_fetch.rs (W12's 4 preserved pins +
    3 added: value form, sparse second-fragment error, mixed
    :?/:- fragments): master 1 passed / 3 failed → 7 passed /
    0 failed
  • Guards: issue1597 14/14, issue1555 12/12, issue614 5/5,
    issue2343 5/5, issue1542 7/7 — all green
  • 35-case GNU 5.3.20 matrix: master 12 same / 21 diff → after fix
    30 same / 5 diff; every shape in the double-evaluation family
    matches GNU, including the value form, case subject, [[ ]] term,
    heredoc value, and the pipeline stage (which printed the :?
    message on master). The 5 remaining diffs are the residuals below.

Residual (unchanged from master, needs a captain ruling)

Command substitution inside a subscript still executes twice
(: ${a[$(echo HIT >&2; echo 0)]:?err} prints HIT twice) — a
comsub's external effects cannot be rolled back by queue hygiene;
fixing that class needs the expander-adjudication or value-handoff
machinery. The issue text names command substitution as an example,
so please rule whether #2558 stays open for that sub-shape or
splits it out. A more severe parse-level defect found on the way
(:? nested in a :- word false-fatals even with literal
subscripts and a set element — a separate mechanism in the scan's
operator parse) is documented in the cluster REPORT for filing.

…2558)

GNU fetches a ${parameter:?word} parameter exactly once:
parameter_brace_expand (subst.c:9777) calls array_value_internal
(arrayfunc.c), which evaluates the subscript inside that single
fetch, and parameter_brace_expand_error (subst.c:8216-8230) expands
the message word only on the error path.

Rubash's command-level error pre-scan is a second, verdict-only
fetch layered over the real expansion: its :?/? arm calls the
evaluating parameter_error_value, whose deferred subscript
evaluation queues its side-effect writes in
PENDING_SUBSCRIPT_WRITES. On the no-error path the real expansion
then fetched again — against an overlay that already contained the
scan's queued write — so : ${a[i++]:?err} incremented i twice,
echo ${a[i++]:?err} returned a[1] instead of a[0], and sequential
commands could overshoot into a genuinely unset element and die on
a fatal the shell manufactured itself.

Fix by isolating the scan's deferred writes at scan granularity:
each top-level scan entry (parameter_expansion_error,
parameter_expansion_error_in_heredoc_body, and the direct word-scan
sites via the new parameter_expansion_error_in_word_isolated) marks
the queue length and, when the scan reaches no error verdict,
truncates the queue back to the mark. The real expansion then
evaluates each subscript exactly once. On an error verdict the
queue is left untouched — the command aborts, the scan's evaluation
is the only one, and state is identical to before. Whole-scan
granularity keeps intra-scan sequencing (later fragments observe
earlier fragments' queued writes, which sparse-array verdicts
depend on); nested windows compose.

Command substitution inside a subscript still executes twice (its
effects cannot be rolled back); see REPORT/issue discussion.

Refs #2558
@caomengxuan666
caomengxuan666 force-pushed the fix/2558-prescan-double-fetch branch from 3f4d33d to 794801e Compare November 2, 2026 10:02
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 10:09
@caomengxuan666
caomengxuan666 merged commit 49e2341 into master Nov 2, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2558-prescan-double-fetch branch November 3, 2026 09:07
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