Skip to content

fix(pattern): empty extglob spans, [[ == ]] backslash, runtime extglob gate - #1179

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/pattern-trio2
Oct 23, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/pattern-trio2

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #1091, #1092, #1093. (1) patsubst skipped zero-length *(...)/?(...) matches: seed with position-independent empty match (match_upattern on NUL-terminated empty suffix), NUL position excluded — 12-cell matrix byte-identical (QQbQQc etc). (2) [[ == ]] non-quoted RHS decoded lexer DATA_BACKSLASH to a real backslash the matcher read as escape: new expand_conditional_pattern_word escorts \x14 via CASE_PATTERN_BACKSLASH_GUARD and restores \ for the matcher; expansion-produced real backslashes keep escape meaning. (3) Whole-file pre-parse froze the extglob gate: script_uses_runtime_parse_gates detects shopt text and routes to the existing grouped driver (incremental gather->run->lex, matching GNU reader_loop; -c/eval/top-level unchanged). Tests: lib 697 + 3 new regression files + 14 related suites (100 items) + shopt suites green; GNU parity byte-identical. Known pre-existing (untouched, separate lane): [[ 'a*b' == a*b ]] strip_conditional_quote_markers CTLESC strip.

…hopt gate

rubash#1091: ${var//*(pat)/rep} skipped empty-string matches.
GNU subst.c pat_subst global loop calls match_pattern per position with
MATCH_ANY, and match_upattern tries p1 down to p itself — the empty match
at each gap counts, with pat_subst:9286-9304 copying one char past a
zero-length match. find_parameter_pattern_match only tried end > start,
so the zero-length arm of the replace loop was dead. Seed each start with
the once-computed empty verdict (position-independent: match_upattern
matches the pattern against the NUL-terminated empty suffix at p), except
at the NUL position itself (pat_subst's `for (...; *str;)` never sits on
it). `${w//*(a)/Q}` on `abac` is now `QQbQQc`.

rubash#1092: unquoted [[ == ]] RHS `a\\b` matched `ab` (and missed the
literal-backslash words). GNU execute_cmd.c COND_BINARY patmatch arm runs
subst.c:4077 cond_expand_word with special=1: no quote removal on the
pattern; the lexer-kept `\\` reaches sm_loop.c:121 as a literal
backslash. The general expansion decoded lex-level DATA_BACKSLASH to a
real `\`, which the matcher read as an escape. Guard it through
expansion with CASE_PATTERN_BACKSLASH_GUARD (proven to survive
expand_word_mut by the `case` arm path) and restore it as `\\`;
expansion-produced `\` keeps its escape meaning.

rubash#1093: runtime `shopt -s extglob` (via eval) did not enable later
`case` extglob arms in script files. GNU shell.c reads scripts
incrementally (reader_loop: parse each command only after previous ones
executed; evalstring.c parse_and_execute), and the shopt builtin flips
extended_glob via reset_parser (parse.y:3502). The whole-file run_source
fast path had already parsed everything. Scripts that may flip the gate
at runtime now take the grouped driver, which lexes each group after the
previous groups ran — the same whitelist-admission shape as
script_uses_aliases. `-c`/eval strings keep the single-parse semantics
GNU gives them (verified: GNU errors there too).
@caomengxuan666

Copy link
Copy Markdown
Member Author

GNU-goldens failure fixed + rebased onto master

Root cause of the goldens failure: driver routing, not glob logic. script_uses_runtime_parse_gates() admitted any script containing the text shopt, so the issue_753 ratchet tests (which start with shopt -s failglob) were routed off the run_source whole-file fast path onto the grouped driver run_script_with_history. The grouped driver's failglob no-match handling only skips the failed command, while GNU (and the fast path) abandon the rest of the logical line — hence the golden diffs (e.g. case2-rc=1 appearing where GNU never prints it). That grouped-driver divergence is real but out of scope here; it is tracked in #1186 and the driver/glob code is untouched.

Fix: narrowed the heuristic to contents.contains("shopt") && contents.contains("extglob") — the only parse-time gate flippable at runtime is extglob via the shopt builtin, so failglob-only scripts keep the fast path again. Doc comment extended accordingly. This PR's new regression tests use shopt -s extglob and still take the grouped driver as intended.

Rebase: the branch conflicted in src/executor/conditional.rs and src/executor/parameter_replace.rs. Both hunks were already-merged duplicates of this PR's own fixes — master's 707892f0 landed the #1092 [[ == ]] backslash fix (DATA_BACKSLASH doubling + quote_aware_glob_rhs) and 893239d1 landed the #1091 empty-extglob-span fix (candidate ends include start). Master's side was taken for both; this PR now contributes the runtime shopt gate plus its regression tests, including two new unit-test modules (conditional_pattern_tests, global_replace_matches_empty_extglob_spans) that pass under master's equivalent implementations.

Verification (local, Windows):

  • cargo test --test regression issue_753 — 17/17 pass (incl. the 6 cases failing in CI run 39814408344)
  • cargo test --test issue1091_1092_1093_pattern_trio_regressions — 3/3 pass
  • cargo test --test regression — 78/78 pass, no golden changes
  • cargo test --lib targeted — retained test modules 3/3 pass

Branch: fix/pattern-trio2 @ 393e792adc8b11aae56dd3c48835af51614b020f (rebased onto b8d2331f).

@caomengxuan666
caomengxuan666 merged commit 4b62893 into master Oct 23, 2026
6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/pattern-trio2 branch October 23, 2026 08:54
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.

[bug] ${var//*(pat)/rep} skips empty-string matches

1 participant