Skip to content

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

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

main @ 9455e9f (crate version 0.0.0)

Language Runtime Version

Rust 1.100.0

Operating System

Amazon Linux 2023 (aarch64)

Installation Method

built from source

Steps to Reproduce

Run each line with strands-shell -c '<script>' and with bash -c '<script>' (bash 5.2.15):

case abc in "a*") echo matched;; *) echo nomatch;; esac
case abc in 'a*') echo matched;; *) echo nomatch;; esac
x="*"; case abc in "$x") echo matched;; *) echo nomatch;; esac
f="notes[1].txt"; case "$f" in "$f") echo same;; *) echo differ;; esac

Expected Behavior

A quoted pattern, or the quoted part of one, matches literally, as in bash:

nomatch
nomatch
nomatch
same

Actual Behavior

matched
matched
matched
differ

The wrong arm runs with no diagnostic. A value compared with itself fails to match when it contains [.

Additional Context

Found while reviewing #325. It is not caused by that PR. I ran a debug build of 4b0bdbc through -c; src/exec.rs and src/parser.rs are identical at main @ 9455e9f.

execute_case (src/exec.rs:2738-2739) expands the pattern with expand_word, which returns a plain string, so the quoting is gone before case_match sees it:

let pattern = expand_word(os.clone(), proc, pat).await;
if case_match(&pattern, &value) {

Possible Solution

glob_match (src/exec.rs) has no escape syntax, so there is no way to hand it a literal *, ? or [. Two options: give glob_match backslash escapes and have the pattern expansion escape the text that came from SingleQuoted/DoubleQuoted parts, or compare with == when every part of the pattern word is quoted (this covers "$x" and 'a*' but not mixed words such as *"$x"*).

Related Issues

#325 ([[ x == "pattern" ]] reuses case_match and has the same gap)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions