Repository navigation
WIP: fix #2558 pre-scan :? double evaluation of subscripted parameters - #2593
Merged
Merged
Conversation
…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
force-pushed
the
fix/2558-prescan-double-fetch
branch
from
November 2, 2026 10:02
3f4d33d to
794801e
Compare
caomengxuan666
marked this pull request as ready for review
November 2, 2026 10:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2558 — do not close on merge; captain adjudicates the residual below.
Defect
The command-level
:?/?error pre-scan fetched a SET subscriptedparameter 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}incrementeditwice;echo ${a[i++]:?err}returneda[1]instead ofa[0]; sequentialcommands 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 thesingle 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 isthe
PENDING_SUBSCRIPT_WRITESqueue. Each top-level scan entry nowmarks 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 nowactually side-effect-free for the write channel, as documented.
Verification (build slot + scrubbed cargo, exact stems)
3 added: value form, sparse second-fragment error, mixed
:?/:-fragments): master 1 passed / 3 failed → 7 passed /0 failed
issue2343 5/5, issue1542 7/7 — all green
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) — acomsub'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 literalsubscripts and a set element — a separate mechanism in the scan's
operator parse) is documented in the cluster REPORT for filing.