Repository navigation
WIP: fix #2210 protect expansion results in the mut word walker (dquote re-scan corruption) - #2215
Merged
Merged
Conversation
…#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.
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 #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 thetest_eval_embedding).Mechanism
expand_embedded_parameters_mut_inner(src/executor/embedded_mutations.rs) finishes with a trailing pass —unescape_remaining_shell_escapesplus\'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 (theprotect_rhs_valueplumbing 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_stringprefixes every character, called fromsubst.c:11862 add_quoted_string:inexpand_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)
expand_embedded_parameters_mut_with_contextnow walks with expansion-result protection on (the existingprotect_rhs_valueport of quote_string: parameter values ride asASSIGN_EXPANSION_BACKSLASH), and the newrestore_rhs_dataflag makesmut_innerrestore 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.\Xpairs 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 theDATA_BACKSLASHcarrier 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):\Xclasses, incl. the 4 always-preserved boundary members)${}/$*siblings; unquoted argument words with arith and with comsub siblings; quoted argument word with a comsub in the trigger role\\pair,\,\$inside values\(/\;in dquotes (assignment + argument + after-\\-pair)local/exportassignments$*eval embedding +sedspy receivess|^\([^/]*/[^/]*\)/.*|\1|byte-for-byte\\/\$/\'semantics unchangedBaseline 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 &selfexpand_wordgate (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.