Skip to content

fix(expand): fix nested arithmetic blowup, @ operator panic, and pattern matcher bugs - #892

Merged
ewhauser merged 2 commits into
mainfrom
claude/elated-elbakyan-35477a
Oct 20, 2026
Merged

ewhauser merged 2 commits into
mainfrom
claude/elated-elbakyan-35477a

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Fixes review findings in arithmetic expansion, parameter expansion, and the shell pattern matchers.

Arithmetic (shell/expand/arith.go)

  • Nested $(( $(( ... )) )) expanded each nested word twice per level (once for the runtime re-parse, once to evaluate), so runtime grew 4x per level (22 levels ≈ 35 s). The value the re-parse already produced is now reused, so expansion is linear. At 1000+ levels the remaining cost is the parser itself (owned by the syntax fixes).
  • x="x+1"; echo $((x)) printed 101 with exit 0. The value re-parse path stopped at depth 100 and treated the rest as 0. It now reports bash's expression recursion level exceeded through the existing 1024-level enterArith limit. The same applies to A[x]="A[x]+1".

Parameter expansion (shell/expand/param.go)

  • ${x@Q"abc"} panicked with unexpected @Qabc param expansion. It now returns ${x@Q"abc"}: bad substitution. The parser still accepts this input; that part is handled in the syntax area.

Pattern matchers (shell/internal/pattern, internal/shellpattern; both copies kept identical)

  • [!a] is negated in the recursive (!(...) / invalid UTF-8) matcher.
  • a**(a|b) no longer collapses ** when the second * opens an extglob group.
  • \é escapes the whole rune, not its first byte.
  • Bracket escapes are literal: [\w] and [\d] no longer turn into Go regexp classes.
  • The recursive matcher kept a full slice of end positions for every state, so *a*a*a*a*a*a*a*a over 1000 as plus one invalid byte took about 20 s. It now runs forward over sets of reachable offsets, so flat patterns are O(tokens·n). Group alternatives are memoized per start offset.

Outside this area: one line in internal/shell/interp/runner.go patternMatch stops discarding the match error. A pattern that fails to compile now matches only its own literal text instead of always being false.

No SPEC changes.

Tests: regression tests in shell/expand and both pattern packages; go test -race ./shell/... ./internal/shellpattern/... passes; make lint is clean.

@ewhauser
ewhauser enabled auto-merge (squash) October 20, 2026 15:04
@ewhauser
ewhauser merged commit a504362 into main Oct 20, 2026
17 checks passed
@ewhauser
ewhauser deleted the claude/elated-elbakyan-35477a branch October 20, 2026 15:14
@ewhauser ewhauser mentioned this pull request Oct 20, 2026
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