Repository navigation
fix(shell): reject unsupported arithmetic expansion (OPE-97) - #240
Conversation
Fail during parsing instead of treating $((...)) as command substitution and silently replacing it with empty output. Preserve literal text and supported expr command substitution, and cover destructive sed/head cases before any write executes. Amp-Thread-ID: https://ampcode.com/threads/T-2cb91949-608b-4c58-a8f1-fb0b2378f182 Co-authored-by: Amp <amp@ampcode.com>
There was a problem hiding this comment.
🟡 Changes recommended
The new $(( guard rejects valid command substitutions beginning with an unspaced subshell, regressing previously supported syntax and requiring disambiguation plus regression coverage.
1 open issue
What changed in this PR
This PR addresses OPE-97 by rejecting unsupported arithmetic expansion during shell parsing, preventing empty expansions from silently changing command behavior or damaging files.
Changes:
- Adds a parse error for
$((...))syntax with guidance to useexpr. - Adds public shell API regression tests for rejection, file preservation, literal text, and supported command substitution.
- Broadens the tokenizer error-field comment to cover word-expansion failures.
| File | Description |
|---|---|
pkg/shell/parser/parser.go |
Adds arithmetic-expansion detection and rejection. |
pkg/shell/parser/lexer.go |
Updates the tokenizer error-field comment. |
pkg/shell/cmds/shell_test.go |
Adds rejection, safety, and compatibility regression tests. |
Review effort: Balanced
| if i+1 < len(s) && s[i+1] == '(' { | ||
| // Arithmetic is not command substitution: treating its body | ||
| // as a subshell silently expands it to an empty string. | ||
| if i+2 < len(s) && s[i+2] == '(' { |
There was a problem hiding this comment.
Confirmed the Bash example and reproduced rejection with a new public-shell regression before the correction. Fixed in 497a049: the guard now runs after matching parentheses, distinguishing adjacent arithmetic closing delimiters from the separated subshell/command-substitution closings. Added quoted and unquoted unspaced-subshell controls with asymmetric expr results. The original arithmetic rejection/file-preservation cases still pass. Shell race tests, repository-wide short race tests, go vet, go build, gofmt, and diff checks passed locally. Pushed; fresh CI is subscribed. PR remains unmerged.
Distinguish arithmetic closing delimiters from command substitutions starting with a subshell. Cover quoted and unquoted forms reported in PR review. Amp-Thread-ID: https://ampcode.com/threads/T-2cb91949-608b-4c58-a8f1-fb0b2378f182 Co-authored-by: Amp <amp@ampcode.com>

Linear issue
Fixes OPE-97.
Diagnosis
The word parser treated
$((...))as$(...)command substitution containing a subshell. With no arithmetic evaluator, it produced an empty string with no reported error. A computed sed address consequently became an unaddressed command, and head received no count. Both could silently corrupt files with exit 0.Fix
Use the issue's explicitly accepted fail-closed option rather than adding an arithmetic engine. Recognize arithmetic expansion in the quote-aware word parser and return a parse error naming the unsupported syntax and suggesting
expr. The existing shell parse-error path exits 2 before any command in the invocation executes, including earlier writes and redirects.Preserve single-quoted and escaped arithmetic text, literal variable values, literal-only heredoc bodies, and normal
$(expr ...)command substitution, including subshells. Cover direct/quoted expansion, assignments, destructive sed/head commands, nested substitution, parameter defaults, loops, redirects, and exports through the public shell API. No sed/head behavior or heredoc semantics changed.Verification
All Go commands ran through
nix develop --command:go test ./pkg/shell/cmds -run '^TestArithmeticExpansion' -count=1failed in all 11 rejection cases. The reportedx=[] exit=0anda=[] b=[]outputs reproduced exactly; sed/head cases returned success instead of errors. Literal and expr-substitution controls passed.go test -race ./pkg/shell/... -count=1passed all shell packages (non-short).go test -race -short ./...passed repository-wide.go vet ./...andgo build ./...passed.gofmtandgit diff --checkpassed.Docker integration suites and dashboard checks were not run locally; this change only affects shell parsing. PR CI remains the delivery gate.
Selection
Selected by Linear issue creation date, oldest first, after skipping unsuitable older unresolved bugs:
Agent thread
Implemented in this scheduled Amp child thread.
Do not merge automatically.