Skip to content

fix(glob,cond): protect full GNU glob-char class at [[ == ]] and pathname-glob boundaries (Fixes #4126) - #4262

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4126-quoted-pattern-metachars
Nov 30, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4126-quoted-pattern-metachars

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Complete the remaining quoted/escaped pattern metacharacter faces for #4126 ([[ == ]] and pathname globbing across single/double/ANSI-C quotes) at the pattern/glob-entry boundaries without widening general lexer token values, and relocate the top-level issue4126 test binary into the issues_3900_4199 bucket (Fixes #4126).

GNU C Specification

  • Conditional [[ == ]]: third_party/bash/pathexp.c:163-196 (glob_char_p() / glob_quote_char()), :212 (quote_string_for_globbing()), subst.c:4077 (cond_expand_word()), execute_cmd.c:3975 (execute_cond_node()).
  • Pathname globbing: third_party/bash/subst.c:4773 (quote_string()), pathexp.c:66 (unquoted_glob_pattern_p()), pathexp.c:163-190 (glob_char_p()), pathexp.c:212 (quote_string_for_globbing()), lib/glob/sm_loop.c:397,413-418 (BRACKMATCH()), subst.c:4807 (dequote_string()).

Changes

  1. Face 2 — src/executor/conditional.rs:
    • Extended append_literal_glob_text to escape the full GNU glob_quote_char class (], -, !, ^, +, @, ., :, =, #, % in addition to *, ?, [, \, (, ), |), so [[ $c == ["^"a] ]], [[ $c == ["!"a] ]], [[ $c == ["[]"] ]], and [[ $c == [a"-"z] ]] match GNU Bash 5.3.20.
  2. Face 4 — src/executor/glob.rs + pathname-glob entry points (command_prepare.rs, command_words.rs, expand_word.rs, pipeline_exec.rs, pipeline_stages.rs):
    • Added strict-whitelist source_literal_glob_pattern in src/executor/glob.rs (admitting only literal-only words with '...', "...", $'...', \c, rejecting expansions/braces/tilde/procsub) that CTLESC-protects quoted characters strictly on the pattern passed to pathname_expand_word, leaving general lexer token values untouched.
    • Extended compound_element_glob_pattern to decode and protect $'...' bytes (a=([a$'\x5d'b])).
    • Wired source_literal_glob_pattern into simple command argv, for words, redirection targets, pipeline stage argv / cat operands, and lastpipe stages.
  3. Tests — tests/issues_3900_4199.rs + tests/issues_3900_4199/issue4126_quoted_pattern_chars.rs:

What this PR deletes

  • Deletes the non-compliant top-level tests/issue4126_quoted_pattern_chars.rs integration binary by relocating it into tests/issues_3900_4199/.

Verification

  • WSL GNU Bash 5.3.20 (/usr/local/bin/bash): 4-face matrix (parameter patterns, case, [[ == ]], casemod, pathname globbing across argv/for/compound arrays/redirections/pipelines with "", '', $'\x5d', $'\x2d', [[":"alpha:]]): 100% byte-exact (0-byte diff).
  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings).
    • cargo test --test issues_3900_4199 issue4126 --locked: 7/7 passed.
    • cargo test --test lexer_quote_tests --locked (unmodified): 16/16 passed.
    • cargo test --test parser_redirection_tests --locked: 68/68 passed.
    • All 10 census-checked families (issue734 15/15, issue868 7/7, issue876 7/7, issue1540 9/9, issue2227 10/10, issue2441 6/6, issue2577 2/2, issue2830 5/5, issue2999 15/15, issue3089 33/33): 0 failures.
  • Windows:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings).
    • cargo test --lib --locked: 1,450/1,450 passed.
    • python scripts/check-rustfmt.py, check-marker-literals.py, check-api-ledger.py: all clean.

Fixes #4126

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — needs rework, cannot merge as-is.

Credit where it's due: the 7 new pins all execute and pass, and every #4126 face is genuinely closed on this tree. The GNU references check out word for word (pathexp.c glob_char_p, parse.y:5694-5706, subst.c quote/dequote_string).

But the census says no: 46 FAIL, 38 of them new regressions, all inside the touched domain — issue2227 ×7, issue2999 ×7, issue868 ×6, issue2830 ×4, issue2441 ×3, issue876 ×3, issue3089 ×2, issue734 ×2, issue1540/2577, plus two single tests. The same-day sibling PRs sit at 7–9 FAIL, so this isn't baseline drift.

Root cause: q22_glob_literal_byte widens the CTLESC marking from "quoted characters in pattern context" to "every such byte inside any lexer quote span". CTLESC is a transport carrier, not part of token values — and the proof is in the PR itself: it had to rewrite existing lexer_quote_tests assertions so token values now contain \u{11}. Once CTLESC leaks into token values, IFS marking, dquote segment scanning, comsub suffix folding, redirect-target metadata and failglob all flip red.

R1 (blocking): narrow the marking surface, or make every downstream consumer CTLESC-aware — pick one, not both. (a) Pull q22_glob_literal_byte back to the three sites #4126 actually needs (push_quoted_pattern_char, conditional.rs append_literal_glob_text, the glob-match entry's pattern side) and leave the rest of the lexer alone; or (b) keep the wide marking and convert all downstream consumers one by one — much bigger and easy to miss one. After rework the census must be back at ≤ baseline (7–11, all known) and the lexer_quote_tests assertions restored (no \u{11} in token values).

R2 (blocking): dequote_ctlesc_pairs_cow needs re-verification with R1. The function itself is semantically right, but its call sites in redirect_operand_transport_text and command_substitution.rs were only exercised under the wide marking; the two single-test pins (test_quoted_redirect_target_records_raw_metadata, test_embedded_process_substitution_stays_in_single_word) must be re-run on the reworked tree.

R3 (blocking or split out): the pipeline_exec stage-expansion refactor. Deleting per-operand glob expansion in the cat segment and trusting stage-expanded argv is a second change bundled into this PR — if expand_pipeline_stage_arg_words doesn't do pathname expansion, printf x | cat f* degrades to opening the literal f*. Either add pins (cat f*, cat "f*", cat -, glob starting with -) or split the refactor into its own PR.

Keep the 7 #4126 pins green through the rework — the faces they close are real.

@caomengxuan666
caomengxuan666 force-pushed the fix/4126-quoted-pattern-metachars branch from be2601c to ecfd424 Compare November 28, 2026 06:48
@caomengxuan666 caomengxuan666 changed the title fix(glob,cond,lexer): protect full GNU glob-char class across [[ == ]] and pathname globbing (Fixes #4126) fix(glob,cond): protect full GNU glob-char class at [[ == ]] and pathname-glob boundaries (Fixes #4126) Nov 28, 2026
@caomengxuan666

Copy link
Copy Markdown
Member Author

Reworked per Captain review (R1(a) + R2 + R3) and rebased onto master (29384e2fe):

Review Resolutions

  • R1(a) — Narrowed CTLESC marking to the glob-entry boundary:
    • Reverted src/lexer/quotes.rs, src/executor/markers.rs, and tests/lexer_quote_tests.rs back to byte-identical origin/master (no \u{11} in general lexer token values; all 16 unmodified lexer_quote_tests pass).
    • Kept Face 2 (src/executor/conditional.rs append_literal_glob_text) at the [[ == ]] pattern boundary.
    • Implemented Face 4 at the pathname-glob entry boundary via a strict-whitelist helper source_literal_glob_pattern in src/executor/glob.rs (GNU subst.c:4773 quote_string / pathexp.c:212 quote_string_for_globbing), plus $'...' decoding/protection in compound_element_glob_pattern, wired into the pathname-glob entry points (command_prepare.rs, command_words.rs, expand_word.rs, pipeline_exec.rs, pipeline_stages.rs).
  • R2 — Reverted speculative command_substitution.rs / expand_word.rs wide-CTLESC changes:
    • src/executor/command_substitution.rs is byte-identical to origin/master; parser_redirection_tests passes 68/68 (including test_quoted_redirect_target_records_raw_metadata and test_embedded_process_substitution_stays_in_single_word).
  • R3 — Split out the pipeline_exec.rs stage-expansion refactor:
    • Reverted the stage-expansion refactor completely; pipeline_exec.rs now only wires source_literal_glob_pattern at its existing pathname-glob sites (+6 / -2).

Verification (WSL Linux + Windows + GNU Bash 5.3.20)

  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings)
    • cargo test --test issues_3900_4199 issue4126 --locked: 7/7 passed
    • cargo test --test lexer_quote_tests --locked (unmodified): 16/16 passed
    • cargo test --test parser_redirection_tests --locked: 68/68 passed
    • All 10 previously-regressed census families: issue734 (15/15), issue868 (7/7), issue876 (7/7), issue1540 (9/9), issue2227 (10/10), issue2441 (6/6), issue2577 (2/2), issue2830 (5/5), issue2999 (15/15), issue3089 (33/33) — 0 failures.
    • 4-face matrix vs WSL GNU Bash 5.3.20 (/usr/local/bin/bash): 100% byte-exact (0-byte diff).
  • Windows:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings)
    • cargo test --lib --locked: 1,450/1,450 passed
    • golden_catfile_comsub, issue_1184_listing_quoting_and_uid_attrs, upstream_suite_slice_snapshots_{quote,nquote,rhs_exp,case}: all passed
    • python scripts/check-rustfmt.py (225 targets), check-marker-literals.py, check-api-ledger.py: all clean.

Fresh CI is running now.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain re-review — rework passed, ready to merge after a rebase.

This is not a narrowing of the old patch — the author rebuilt the approach entirely. The wide lexer marking (q22_glob_literal_byte) is gone, "lexer" is out of the title, and the new mechanism is "glob entry-boundary rebuild protection": glob::source_literal_glob_pattern(raw) decides whether a source word is literal-only (rejecting $ expansions except $'...', backticks, {}, ~, $(, <(/>(, $/backtick inside double quotes, unterminated quotes, otherwise falling back to the expanded word as-is), then compound_element_glob_pattern (with a new $'...' decoding arm, per GNU subst.c:4773) rebuilds a protected pattern, wired at all 6 glob entry points.

  • R1 — passed. Zero lexer changes; lexer_quote_tests.rs has no \u{11} anywhere (old assertions restored). Census is back to 8 FAIL, all known (issue2024 / issue2340 / issue2698 / issue3300 / issue3420 / issue3575 / issue3605 / issue4181), 0 SIGABRT, goldens only the 2 standing snapshots — the 38 new regressions are all gone.
  • R2 — passed. dequote_ctlesc_pairs_cow is existing master code, not wired anywhere new; both pins ran PASS in the census log.
  • R3 — passed (closed by removal). The bundled stage-expansion refactor is gone (pipeline_exec diff now only +8/−2). cat keeps its own per-operand expansion, and expand_pipeline_stage_arg_words already did per-operand expansion on base. No new cat f* pin was added, but the premise (deleting the expansion) no longer exists; adding one is a non-blocking follow-up suggestion.

The 7 #4126 pins all ran PASS by name in the census log, plus new conditional/pathname/[[:alpha:]] coverage and lib unit pins.

One remaining step before merge: please rebase onto current master. The PR base is still 29384e2f; master is now a4e529f9 (#4260 just merged). Once rebased, I'll re-check mergeability and the new CI run — if census is still the same known set, it merges right away and closes #4126.

@caomengxuan666
caomengxuan666 force-pushed the fix/4126-quoted-pattern-metachars branch from ecfd424 to 462cf87 Compare November 30, 2026 00:36
@caomengxuan666

Copy link
Copy Markdown
Member Author

Rebased onto master 5ff7280 as 462cf87 (clean, no conflicts). Windows: lib -D warnings clean, issue4126 7/7, rustfmt/api-ledger ok. Awaiting fresh CI census.

@caomengxuan666
caomengxuan666 merged commit a72db66 into master Nov 30, 2026
11 of 13 checks passed
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.

${x//pat/…}, ${x#pat}: a quoted or escaped ], -, !, ^, #, % keeps its pattern meaning

1 participant