Repository navigation
fix(expand): fix nested arithmetic blowup, @ operator panic, and pattern matcher bugs - #892
Merged
Merged
Conversation
Also turn the unknown @ operator panic into a bad substitution error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes review findings in arithmetic expansion, parameter expansion, and the shell pattern matchers.
Arithmetic (
shell/expand/arith.go)$(( $(( ... )) ))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))printed101with exit 0. The value re-parse path stopped at depth 100 and treated the rest as 0. It now reports bash'sexpression recursion level exceededthrough the existing 1024-levelenterArithlimit. The same applies toA[x]="A[x]+1".Parameter expansion (
shell/expand/param.go)${x@Q"abc"}panicked withunexpected @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.[\w]and[\d]no longer turn into Go regexp classes.*a*a*a*a*a*a*a*aover 1000as 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.gopatternMatchstops 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/expandand both pattern packages;go test -race ./shell/... ./internal/shellpattern/...passes;make lintis clean.