Skip to content

fix(case): match quoted parts of a pattern literally - #351

Merged
ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix-327-case-quoted-pattern
Nov 26, 2026
Merged

ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix-327-case-quoted-pattern

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

case treated a quoted pattern as a glob, so case abc in "a*") took the a* arm, and so did a quoted $x holding *. That also meant a value compared with itself, like case "$f" in "$f"), didn't match when it contained [, so the wrong arm ran with no error.

Now the quoted parts of a case pattern match literally and the unquoted parts still glob, the same way [[ == ]] already handles its right-hand side. Both places that run case (normal and inside a command substitution) get the fix.

Related Issues

Fixes #327

Documentation PR

N/A

Type of Change

Bug fix

Testing

I added a differential case covering single and double quotes, quoted and unquoted variables, mixed patterns like a"*", and the self-match with brackets. On main it failed, and with the change it passes along with the full test suite. I left out backslash-escaped patterns like a\*, which still glob because the backslash is dropped earlier during parsing. That needs its own fix.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@github-actions github-actions Bot added the bug Something isn't working label Nov 21, 2026

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested. The quoted-pattern fix works and matches bash, but it breaks an unquoted \\ in a case pattern (case $p in *\\*)), which main gets right. The fix is three lines; I built and ran it.

Result at 94da1f4
Claimed fix: quoted parts literal, both case sites ✅ matches bash 5.2.15; the new case fails on dbb6189 and passes here
🔴 Unquoted \\ in a case pattern regression: silently takes the wrong arm (inline)
🟡 Quoted glob char inside [...], e.g. ["*?"] regression: the set also matches a backslash (inline)
Suites cargo test --workspace --all-targets: 2733 passed, 0 failed; cargo fmt --check and cargo clippy exit 0
What I verified, and non-blocking notes

Verified

  • Head 94da1f4, base dbb6189 (= main), debug builds of both, oracle bash 5.2.15 on aarch64 Linux.
  • case_quoted_pattern_is_literal.expected is byte-identical to what bash prints for the .sh (run with the README's empty environment). Base differs on 7 of the 11 lines; head on none.
  • A 163-script probe corpus, each run directly and inside $(...) (326 runs), against bash, base and head. Head disagrees with bash where base agrees in 56 runs. All 56 come from three causes: the two inline findings and the tilde note below.
  • With the candidate fix from the two inline comments applied on top of 94da1f4: 54 of those 56 runs match bash, no run gets worse, and cargo test --workspace --all-targets is still 2733 passed, 0 failed. The remaining 2 are the tilde case.
  • Not run: pytest tests/python, npm test (the change is Rust-only; CI is green for both).

Good side effect worth a test line. An unquoted variable holding a backslash now behaves like bash: x='a\b'; case ab in $x) matches and case 'a\b' in $x) doesn't. main has both backwards. The new case doesn't cover it.

Appendix: non-blocking (4)

  • ⚪ expand_pattern skips the is_array_literal → refuse_array check that expand_word has (src/exec.rs:498-501). case x in a=(1 2)) echo z;; esac printed arrays are not supported and exited 2 on main; here it's silent and exits 0. It's a syntax error in bash, so only the loud refusal is lost.
  • ⚪ Tilde output isn't escaped: HOME='/tmp/a\b'; case '/tmp/a\b' in ~) matches in bash and on main, not here. Adding WordPart::Tilde(_) to the quoted arm should cover it (not built).
  • ⚪ Same on main and here, so not from this PR: quotes nested in an expansion still glob (unset u; case abc in ${u:-"a*"}) matches; bash doesn't).
  • ⚪ Same on main and here: quoted !, ^, -, ] inside a bracket keep their special meaning (case b in ["!"a]) matches; bash doesn't).

Comment thread src/exec.rs Resolved
Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at d26b9cc. Both findings from my last review are fixed and verified. One rare regression is left, and it comes from the bracket code I suggested.

Previous finding Status at d26b9cc
🔴 Unquoted \\ in a case pattern fixed: the repro matches bash, and [[ $p == *\\* ]] now does too
🟡 Quoted glob char inside [...] fixed, but ranges with an escaped endpoint ([A-\\], ["*"-z]) broke (new 🟡, inline)
⚪ Tilde output, array-literal refusal both behave like main again

The new 🟡 needs unusual input, so I'm not blocking on it. The inline comment has a fix I built and ran, if you want it in this PR.

What I verified, and non-blocking notes

Verified

  • Head d26b9cc, base dbb6189 (= main), debug builds, oracle bash 5.2.15 on aarch64 Linux.
  • cargo test --workspace --all-targets: 2733 passed, 0 failed. cargo fmt --check and cargo clippy --workspace --all-targets exit 0. CI is green.
  • case_quoted_pattern_is_literal.expected is byte-identical to what bash prints for the .sh (23 lines, README's empty environment). dbb6189 differs on 9 lines, 94da1f4 on 7, d26b9cc on none.
  • Both repro scripts from my first review match bash line for line.
  • The 326-run probe corpus from the first review: the 56 runs that regressed at 94da1f4 all match bash now, and no run got worse.
  • The array refusal is back: case x in a=(1 2)) echo z;; esac prints arrays are not supported and exits 2, as on main.
  • 75 new scripts aimed at this delta (backslash before various characters, brackets, tilde, the [[ ]] path), each run directly and inside $(...). Two causes differ from bash where main agrees: the range finding inline, and the note below.
  • Not run: pytest tests/python, npm test.

Appendix: non-blocking (1)

  • ⚪ An unquoted \\ inside a ${...} default is still read as an escape: unset u; case 'a\b' in ${u:-a\\b}) matches in bash and on main, not here (same at 94da1f4). The Literal fix only looks at top-level parts. I don't expect real scripts to hit this.

Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at ed589e4. The range-endpoint finding is fixed and verified, and this round turned up nothing new in the change.

Previous finding Status at ed589e4
🟡 Bracket range with an escaped endpoint fixed: all nine repro lines match bash, in case and in [[ ]]
⚪ \\ inside a ${u:-a\\b} default unchanged by choice; still the only place I found where this PR is worse than main

Deferring the ⚪ is fine by me. It needs the same nested-quoting information as ${u:-"a*"}, which is already wrong on main, so the two are best fixed together.

What I verified
  • Head ed589e4, base dbb6189 (= main), debug builds, oracle bash 5.2.15 on aarch64 Linux.
  • src/exec.rs at ed589e4 is byte-identical to the build I tested last round (blob 6f352e0).
  • cargo test --workspace --all-targets: 2733 passed, 0 failed. cargo fmt --check and cargo clippy --workspace --all-targets exit 0.
  • case_quoted_pattern_is_literal.expected is byte-identical to what bash prints for the .sh (30 lines). d26b9cc differs on 4 lines, ed589e4 on none.
  • The repro scripts from all three inline comments match bash line for line.
  • Probe scripts, each run directly and inside $(...): the 163 from round one, the 75 from round two, and 52 new ones on bracket forms ([]a], [!]a], [a-], ranges with escaped endpoints, unquoted variables holding \) plus callers that use the matcher with escapes off (${x#[a-c]}, ${x%%[0-9]*}). The only runs where main agrees with bash and this branch doesn't are the two for ${u:-a\\b}.
  • CI: every build and test job and CI Gate succeeded. The rollup shows red only because one duplicate 2 Approvers for Bots run was cancelled; its rerun passed.
  • Not run: pytest tests/python, npm test.

Appendix: non-blocking (1)

  • Pre-existing, filed #365: unsupported ${var...} operators such as ${x//a/_}, ${x^^} and ${x:1:3} are silently dropped. Unrelated to this change; I ran into it while probing the matcher's other callers.

@ferdingler
ferdingler merged commit 4f838f9 into strands-agents:main Nov 26, 2026
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] case matches a quoted pattern as a glob (case abc in "a*") matches)

4 participants