Skip to content

WIP: fix #2210 protect expansion results in the mut word walker (dquote re-scan corruption) - #2215

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2210-arith-dquote-rescan
Oct 30, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2210-arith-dquote-rescan

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2210.

Defect

When a double-quoted string contains an arithmetic expansion $((...)) and a sibling expansion (variable / ${} / $* / positional) whose value contains backslashes, the constructed value was re-scanned and backslashes before shell-token characters were dropped (\(, \), \$, \\, \", \`, \;, \|, backslash-space). This is the mechanism that eats git test-lib bodies wholesale (t0060 #84/#85/#86 via the test_eval_ embedding).

Mechanism

expand_embedded_parameters_mut_inner (src/executor/embedded_mutations.rs) finishes with a trailing pass — unescape_remaining_shell_escapes plus \' collapses — gated on the source word containing $( or a backtick. That pass exists to resolve SOURCE escape pairs the segment scanner deliberately defers, but it runs over the fully assembled string, where it cannot distinguish deferred source pairs from backslash bytes that arrived as expansion data. Parameter-expansion results were spliced into the output unprotected (the protect_rhs_value plumbing existed but was enabled only for the unquoted assignment-RHS entry, rubash#218), so the pass ate their backslashes. A sibling trigger in argument position via a comsub hit the same gate; comsub output itself was already carrier-protected, which is why it survived as a sibling and why "no $( in the word" was always clean.

GNU's invariant: an expansion result merged into a word is CTLESC-protected wholesale — subst.c:4773 quote_string prefixes every character, called from subst.c:11862 add_quoted_string: in expand_word_internal — and quote removal (subst.c:4807 dequote_string) only strips CTLESC bytes. Assembled data is never re-scanned as escape syntax.

Fix (one file: src/executor/embedded_mutations.rs)

  1. expand_embedded_parameters_mut_with_context now walks with expansion-result protection on (the existing protect_rhs_value port of quote_string: parameter values ride as ASSIGN_EXPANSION_BACKSLASH), and the new restore_rhs_data flag makes mut_inner restore the marker right after its own trailing unescape pass — the same protect/restore split the assignment-RHS entry uses, with the restore at this layer because these callers consume the output as final text. Compound/alternate/here-string entries are unchanged.
  2. Second face of the same gate: SOURCE-literal \X pairs inside a double-quoted span (X outside the CBSDQUOTE set, syntax.h:58) are literal data in GNU, but the scanner emitted them raw and the same trailing pass ate them ("x=$((1+1)) a\(b" -> a(b). The scanner now carries such a backslash as the DATA_BACKSLASH carrier through the walk — including the survivor of a resolved \\ pair ("a\\(b" -> a\(b) — and the walker's existing tail chain restores it after the pass. The line-continuation pair is excluded.

No change to the unescape pass itself, no per-shape guards: source pairs in unquoted spans still resolve exactly as before (overcorrection guards in the pins).

Pins

tests/issue2210_arith_dquote_rescan_regressions.rs (script-file harness, GNU bash 5.3.20 expectations):

  • minimal repro (assignment, arith + plain-var sibling)
  • the full corruption matrix (13 \X classes, incl. the 4 always-preserved boundary members)
  • argument position; arith after the sibling; ${}/$* siblings; unquoted argument words with arith and with comsub siblings; quoted argument word with a comsub in the trigger role
  • data classes: \\ pair, \ , \$ inside values
  • source-literal \( / \; in dquotes (assignment + argument + after-\\-pair)
  • local / export assignments
  • the test-lib shape end-to-end: $* eval embedding + sed spy receives s|^\([^/]*/[^/]*\)/.*|\1| byte-for-byte
  • boundary controls (already green on master): no-arith, comsub sibling, unquoted assignment RHS, heredoc
  • overcorrection guards: unquoted source escapes still resolve; dquote source \\/\$/\' semantics unchanged

Baseline on master (80bf195): 9 failed / 2 passed (the two control tests). With this change: 11 passed / 0 failed / 1 ignored; lib tests 983 passed; nine adjacent quote/assignment/expansion targets all green; the triage probe battery (probe-h, probe-matrix, probe-exact, minimal repro) is byte-identical to GNU.

Parked sibling face (not in this PR)

The redirect-target pin is #[ignore]d: redirect operands expand through the &self expand_word gate (expand_word.rs) over the NON-mut walker, whose splice sites carry no protection and whose braced-expansion tree is separate — the same mechanism in the sibling walker. Evidence and the recommended port shape are in the cluster report for a follow-up issue.

…#2210)

An arithmetic expansion sharing a double-quoted string with a sibling
expansion corrupted the sibling's backslashes: the mutable word
walker's trailing unescape pass (gated on the source word containing
`$(` or a backtick) ran over the fully assembled word and read
expansion DATA as source escape syntax, dropping `\` before
shell-token characters (\( \) \$ \\ \" \` \; \| and backslash-space).

GNU never re-scans assembled text: subst.c:4773 quote_string
CTLESC-prefixes every character of a parameter-expansion result
merged into a word (subst.c:11862 add_quoted_string in
expand_word_internal), and subst.c:4807 dequote_string only strips
CTLESC bytes. Port that invariant to
expand_embedded_parameters_mut_with_context the way the
assignment-RHS entry already did for rubash#218: parameter results
ride as ASSIGN_EXPANSION_BACKSLASH through the walk, and the entry
restores them after its own trailing pass (new restore_rhs_data
flag; the assignment-RHS entry keeps its caller-side restore).

Same gate, second face: source-literal \X pairs inside a
double-quoted span (X outside the CBSDQUOTE set, syntax.h:58) are
literal data in GNU; the scanner now carries their backslash as
DATA_BACKSLASH through the walk (including the survivor of a
resolved \\ pair), restored by the walker's existing tail chain.

Refs #2210. Pins: tests/issue2210_arith_dquote_rescan_regressions.rs
— minimal repro + full corruption matrix + sibling forms + boundary
controls; 9 red on master, all green with this change. The
redirect-target pin is #[ignore]d: redirect operands expand through
the &self expand_word gate over the non-mut walker — the same
mechanism in the sibling walker — documented in the cluster report
for a follow-up.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 12:31
@caomengxuan666
caomengxuan666 merged commit 01836d3 into master Oct 30, 2026
12 of 13 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2210-arith-dquote-rescan branch October 30, 2026 12:31
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