Skip to content

WIP: fix nounset pre-scans judging procsub bodies (#1915) - #1929

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/1915-procsub-setu
Oct 28, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/1915-procsub-setu

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Refs #1915

Under set -u, mapfile -t x < <(printf '10\n' | awk '$1 == 10 {print $1}') died with $1: unbound variable: the parent-side nounset pre-scans walked into <(...) / >(...) span text and judged the body's single-quoted $1 as a live parent reference.

GNU never expands a procsub body in the parent: expand_word_internal (subst.c:11366-11385) extracts it verbatim via extract_process_subst (subst.c:1311) and process_substitute (subst.c:6362) hands it to the child shell, whose own parse owns every $ inside.

Fix by mechanism: the pre-scans (simple-command words/assignments in parameter_expansion_error and its pipeline twin, plus the redirect-target gate in redirection.rs) now mask out exactly the process-substitution spans the parser recorded for the command (verbatim child text) before judging. Look-alike <(...)-shaped text that is not a recorded span — e.g. inside double quotes, where $ stays a live parent reference — is still judged in full, and arithmetic text (no spans recorded there) is untouched.

Verification (base master 4987def after rebase, heads a4f14dd + 9962fbf):

  • Differential matrix vs GNU bash 5.3.20 oracle (script files): 34/34 in-scope shapes byte-identical (issue repro, word/redirect/assignment positions, nested/quoted-paren bodies, output spans) plus all still-fires guards (plain unbound, unbound after a span, arithmetic group, double-quoted fake spans in word and assignment position, real+fake mixed on one line).
  • New regression file tests/issue1915_procsub_nounset_regressions.rs: 17/17 pass.
  • cargo test --lib: 899 passed / 1 failed (ownership_unary_operators_check_existing_file, pre-existing on master) / 3 ignored — zero new reds.
  • diffcorpus 306: 306/306 PASS (two timing-flake singles — ps06 job timing, pf203 wall-clock printf — seen in intermediate runs only, each passing on isolated reruns).
  • cargo check with -D warnings clean; per-file rustfmt clean; CR count 0.

File overlap with perf-f6 (read path): none — this change touches only src/executor/parameter_errors.rs and src/executor/redirection.rs (gate call site).

t7 follow-up: the [[ ]] operand scans (second commit, 9962fbf)

The residual recorded in the cluster report: set -u; [[ -n <(awk '$1' <<< '10') ]] still died with $1: unbound variable — the conditional operand scans are different scan points from the simple-command pre-scan and held no span records. GNU expands [[ ]] operands through cond_expand_word (cond.c) -> expand_word, whose '<'/'>' cases extract the span verbatim exactly as for simple words, so the parent never judges the body on this face either.

The parser had the records all along: ConditionalCommand.arg_metadata is aligned with args, and each WordMetadata carries that word's recorded process_substitutions. execute_conditional_command now publishes (operand text -> recorded span targets) into Executor::conditional_procsub_span_targets (save/restore), and the two conditional nounset scan points — expand_conditional_word's expansion-error scan and regex_operands_nounset_fatal — mask exactly the spans recorded for the word under scan through the shared mask_recorded_span_targets core (split out of mask_recorded_process_substitutions; the simple-command behavior is unchanged). Lookup is by the word's exact text, so the q3 precision holds on this face: [[ -n "<(awk '$NOPE')" ]] and [[ "<(awk '$NOPE')" == <(awk '$NOPE') ]] still raise the GNU fatal.

Verification (second commit): regression target 23/23 (6 new conditional pins: span operands for -n/-z/>(...)/=~ RHS pass; bare/quoted/=~-side unbound guards and the beside-a-span guards keep their fatals); lib 899/1/3, failure set identical to the pristine baseline; diffcorpus 306/306; -D warnings clean; per-file rustfmt clean; CR 0.

Recorded residuals (different pre-existing mechanisms, NOT nounset scans — see cluster REPORT section 7): on the [[ ]] face a procsub span used as a comparison VALUE is not materialized to its /dev/fd string (the operand text compares literally), which leaves an observable delta for span subjects of ==/=~ and for the child-side diagnostic of a body like <(awk "$1" ...); and the conditional parser rejects any suffix after a word-initial span (<(...)x). The nounset dimension of all three shapes is fixed by this PR (no parent fatal, script continues, script rc matches GNU).

…1915)

Under `set -u`, `mapfile -t x < <(printf '10\n' | awk '$1 == 10
{print $1}')` died with `$1: unbound variable`: the parent-side
nounset pre-scans walked into `<( ... )` / `>( ... )` span text and
judged the body's single-quoted `$1` as a live parent reference.

GNU never expands a procsub body in the parent: expand_word_internal
(subst.c:11366-11385) extracts it verbatim via extract_process_subst
(subst.c:1311) and process_substitute (subst.c:6362) hands it to the
child shell, whose own parse owns every `$` inside.

Fix by mechanism: the pre-scans (simple-command words/assignments in
parameter_expansion_error and its pipeline twin, plus the
redirect-target gate in redirection.rs) now mask out exactly the
process-substitution spans the parser recorded for the command
(verbatim child text) before judging. Look-alike `<(...)`-shaped
text that is not a recorded span — e.g. inside double quotes, where
`$` stays a live parent reference — is still judged in full, and
arithmetic text (no spans recorded there) is untouched.

Regression tests: tests/issue1915_procsub_nounset_regressions.rs
(17 tests, GNU 5.3.20 expectations).
…#1915)

The t7 residual of #1915: the simple-command pre-scans masked a
command's recorded process-substitution spans, but the [[ ]] operand
scans judged the raw word, so [[ -n <(awk '$1' <<< '10') ]] under
set -u still died with '$1: unbound variable'. GNU cond_expand_word
(cond.c) expands operands through expand_word (subst.c:11229), whose
'<'/'>' cases extract the span verbatim (subst.c:1311
extract_process_subst) for process_substitute (subst.c:6362) — the
parent never judges the body on this face either.

Thread the records the parser already made: ConditionalCommand
carries arg_metadata aligned with args, each WordMetadata holding
the word's recorded process_substitutions. execute_conditional_command
publishes (operand text -> recorded span targets) into
Executor::conditional_procsub_span_targets (save/restore), and the
two conditional nounset scan points — expand_conditional_word's
expansion-error scan and regex_operands_nounset_fatal — mask exactly
the spans recorded for the word under scan via the shared
mask_recorded_span_targets core. The criterion is unchanged (only
parser-recorded child text is masked): a quoted look-alike span in a
sibling operand was never recorded for that word, so its $ stays a
live parent reference and still fires. No scan predicate, no
conditional expansion code changes.

Verification (this commit, base master 4987def): regression target
23/23 (6 new conditional pins, GNU 5.3.20 byte-checked expectations);
lib 899/1/3 identical to the pristine baseline failure set;
diffcorpus 306/306; -D warnings clean; per-file rustfmt clean; CR 0.
The differential matrix (original 34 shapes + the t7 group) is
byte-SAME except three t7 shapes whose remaining delta is a
different, pre-existing mechanism (conditional operands are not
materialized to /dev/fd values), recorded in the cluster REPORT S7.
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