Skip to content

WIP: fix(case): keep escape pairs intact in quote-free pattern expansion (#2421) - #2427

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2421-case-escaped-quote-pattern
Nov 1, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2421-case-escaped-quote-pattern

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2421 — case patterns beginning with \" (and the whole escaped-quote / escaped-dollar family in quote-free patterns) never matched.

GNU contract

  • A case pattern undergoes no quote removal (execute_cmd.c:3733-3736): read_token_word consumes \X as one quoted unit at read time (parse.y:5366-5398 — the backslash branch sets pass_next_character; parse.y:5358-5362 routes the next char to got_escaped_character, which CTLESC-marks it).
  • The pattern word is expanded with expand_word_leave_quoted (execute_cmd.c:3712), then quote_string_for_globbing (pathexp.c:212) re-emits the quoting for strmatch: 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.
  • The [[ == ]] 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 to expand_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-pattern a\"b, bracket member [\"a], and x\"*y all diverged.
  • A spelled-out \$ 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).
  • A \ 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_rhs already walks a [[ == ]] RHS (that face was probed clean — it never had this bug):

  • \X pairs 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.
  • The lexer's data-quote carriers \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 \x18 belongs to strings the parameter machinery hands the matcher directly, not to lexer word text passing through expansion.)
  • Runs go through the same expand + decode_parameter_pattern_quotes pipeline as before (factored into expand_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 test results 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.

  • Fixed binary (foreground build, artifact verified) vs GNU on 8 probe files covering the family — seeds, start/mid/end/alone escapes, \$ (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).
  • Unit: case_arm_static_tests 4/4 pass, including the new escquote_2421_shapes_expand_like_gnu (asserts both the spelled-out pair and the \x18/\x17 carrier spellings decode to the matcher escape form).
  • Neighbor suites green: case_command_head, issue1393, issue1401, issue1641, issue1656, issue1091_1092_1093, issue1114, issue1281, issue1029_1067_1241_1253.
  • cargo check --tests clean; 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, because decode_parameter_pattern_quotes reads 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.

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
caomengxuan666 merged commit fb792b2 into master Nov 1, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2421-case-escaped-quote-pattern branch November 2, 2026 01:09
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