Repository navigation
WIP: fix(case): keep escape pairs intact in quote-free pattern expansion (#2421) - #2427
Merged
Merged
Conversation
Refs #2421 GNU contract: a case pattern undergoes no quote removal (execute_cmd.c:3733-3736) because read_token_word consumes `\X` as one quoted unit at read time (parse.y:5366-5398 backslash branch sets pass_next_character; parse.y:5358-5362 routes the char to got_escaped_character, which CTLESC-marks it), and after expand_word_leave_quoted (execute_cmd.c:3712) quote_string_for_globbing (pathexp.c:212) re-emits that quoting for strmatch. The escaped character is literal; an unquoted glob after it stays live. The `[[ == ]]` RHS goes through the same conversion (subst.c:4077 cond_expand_word, special==1 -> subst.c:4124). Root cause: the quote-free branch of expand_case_pattern protected only the backslash (guard byte) and fed the rest to expand_word_mut, splitting every escape pair before the expander: - `\"` in word text is the lexer's DATA_DQUOTE carrier (\x18, the backslash already consumed); guarding it as a backslash collapsed `\"*` to `\*` (literal star), so the seed patterns never matched. - A spelled-out `\$` pair lost its backslash to the guard, so the bare `$` started a real parameter expansion (`\$*` became whatever `$*` expanded to in the current context). - A `\` inside a `${...}` default was guard-rewritten before the unit's own grammar (parse.y:3877 parse_matched_pair) could see it, breaking `${v:-\"*}`. Fix: walk the pattern text the way quote_aware_glob_rhs walks a `[[ == ]]` RHS. `\X` pairs outside nested expansion units are emitted verbatim for the matcher; `$(...)`/`${...}`/backtick units stay whole inside the runs between pairs (their inner backslashes are theirs); the data-quote carriers \x18/\x17 decode to the matcher's escape spelling for the literal quote. Runs go through the same expand + decode_parameter_pattern_quotes pipeline as before (new expand_case_pattern_run helper). The quoted-segment branch (patterns containing real quotes) and the matcher were probed clean and are untouched, as were the `[[ == ]]` and ${var/pat/} faces. Regression: tests/issue2421_case_escaped_quote_pattern_regressions.rs (6 script-level tests, expectations measured against GNU bash 5.3.20) plus the escquote_2421_shapes_expand_like_gnu unit test in case_arm_static_tests covering both the spelled-out pair and the carrier spellings.
caomengxuan666
marked this pull request as ready for review
November 1, 2026 03:15
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 #2421 —
casepatterns beginning with\"(and the whole escaped-quote / escaped-dollar family in quote-free patterns) never matched.GNU contract
read_token_wordconsumes\Xas one quoted unit at read time (parse.y:5366-5398 — the backslash branch setspass_next_character; parse.y:5358-5362 routes the next char togot_escaped_character, which CTLESC-marks it).expand_word_leave_quoted(execute_cmd.c:3712), thenquote_string_for_globbing(pathexp.c:212) re-emits the quoting forstrmatch: a CTLESC-quoted glob char comes back backslash-escaped, a quoted non-glob char (e.g.") comes back bare — literal either way — and unquoted glob chars stay live.[[ == ]]RHS uses the identical conversion (cond_expand_word, subst.c:4077; special==1 → subst.c:4124).So
\"*must behave as: literal"+ live glob*.Root cause
The quote-free branch of
expand_case_pattern(src/executor/compound_exec.rs) protected only the backslash (guard byte) and fed the rest toexpand_word_mut, splitting every escape pair before the expander:\"in word text is the lexer's DATA_DQUOTE carrier (\x18— the backslash is already consumed at read time; verified by codepoint dump of the stored pattern: text =[0x18, 0x2a]). Guarding that carrier as if it were a backslash collapsed\"*→\*(literal star only). Both filed seeds,\"alone, mid-patterna\"b, bracket member[\"a], andx\"*yall diverged.\$pair (text keeps\$) lost its backslash to the guard, so the bare$began a real parameter expansion:\$*silently became whatever$*expanded to in the current context (inside a function, the function's own arguments — the arm matched the word itself).\inside a${...}default was guard-rewritten before the unit's own grammar (parse.y:3877 parse_matched_pair) saw it:${v:-\"*}never matched.Measured pre-fix finals (discriminator probes vs GNU):
\"*→\*,\$*→context-dependent expansion,a\"b→a\b(≡ab),\"→\,*\"→*\.Fix
Walk the pattern text the way
quote_aware_glob_rhsalready walks a[[ == ]]RHS (that face was probed clean — it never had this bug):\Xpairs outside nested expansion units are emitted verbatim for the matcher — the expander never sees them.$(...)/${...}/ backtick units stay whole inside the runs between pairs; their inner backslashes belong to the unit's own grammar.\x18/\x17(its spelling of\"/\'in word text) decode to the matcher's escape spelling for the literal quote. (The pattern-domain PATTERN_LITERAL_BACKSLASH reading of\x18belongs to strings the parameter machinery hands the matcher directly, not to lexer word text passing through expansion.)decode_parameter_pattern_quotespipeline as before (factored intoexpand_case_pattern_run); the mutable expander is kept, so$((x=1))side effects in patterns are preserved.Untouched, after probing them clean: the quoted-segment branch (patterns with real quotes —
a"b"\*,a"b"\"*,\"*"x"all GNU-identical), the matcher itself, the[[ == ]]face, and the${var/pat/}face (patsubst probe GNU-identical).Verification
Expectations all measured against the GNU bash 5.3.20 oracle with script files. A note on method: the first round of
cargo testresults in this session arrived through backgrounded executions whose artifacts never landed in this filesystem view (the phantom pattern from AGENTS.md); those results were discarded and everything below was re-verified with foreground builds whose artifacts were checked on disk.\$(incl. inside a function),\a/\\/\[/\*/\`/\/\'controls, bracket members,${v:-\"*},$w\"*, quoted-segment shapes,[[ == ]]faces, variable patterns, patsubst: byte-identical output, except one pre-existing divergence listed under Findings.tests/issue2421_case_escaped_quote_pattern_regressions.rs: 6/6 pass (genuine run).case_arm_static_tests4/4 pass, including the newescquote_2421_shapes_expand_like_gnu(asserts both the spelled-out pair and the\x18/\x17carrier spellings decode to the matcher escape form).cargo check --testsclean; rustfmt clean on both touched files; 0 CR bytes.Left for the main session's scrubbed batch (standing workflow): re-run of the new pin targets under the scrubbed-fd runner before merge.
Finding (not fixed here — separate mechanism)
A pattern taken from a variable whose value contains quote characters diverges from GNU, identically on master and on this branch (so not a regression):
p='"*"'; case '"xb"' in $p)— GNU matches via$p(expansion results carry no quoting: the"s are plain data and the*between them is a live glob); rubash falls through to the next arm, becausedecode_parameter_pattern_quotesreads quote spans in the expansion result as pattern quoting. Root area: pattern preparation for expansion results, a different code path from this fix; filed for the main session to adjudicate.