Skip to content

fix(shell): reject unsupported arithmetic expansion (OPE-97) - #240

Merged
aakarim merged 2 commits into
mainfrom
fix/ope-97-reject-arithmetic-expansion
Nov 6, 2026
Merged

aakarim merged 2 commits into
mainfrom
fix/ope-97-reject-arithmetic-expansion

Conversation

@aakarim

@aakarim aakarim commented Nov 5, 2026

Copy link
Copy Markdown
Owner

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:

  • Before the fix: go test ./pkg/shell/cmds -run '^TestArithmeticExpansion' -count=1 failed in all 11 rejection cases. The reported x=[] exit=0 and a=[] b=[] outputs reproduced exactly; sed/head cases returned success instead of errors. Literal and expr-substitution controls passed.
  • After the fix: the same regression command passed. Destructive cases leave fixture content byte-for-byte unchanged; no command output is produced on rejection.
  • go test -race ./pkg/shell/... -count=1 passed all shell packages (non-short).
  • go test -race -short ./... passed repository-wide.
  • go vet ./... and go build ./... passed.
  • gofmt and git diff --check passed.

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:

  • OPE-65: historical Claude MCP transport failure lacks server-side evidence to identify a safe fix.
  • OPE-74: the issue comment confirms genuinely distinct case-sensitive directories; changing analytics to merge these requires a case/content policy decision.
  • OPE-77 and OPE-88: already In Progress.
  • OPE-89, OPE-91, and OPE-94: In Review with open implementation PRs.
  • OPE-90: literal-only heredocs are explicitly intentional in the AST/lexer and await a maintainer decision.
  • OPE-95: Escape already has a handler and regression coverage in the fuzzy finder; the title-only report provides no failing context to isolate safely.

Agent thread

Implemented in this scheduled Amp child thread.

Do not merge automatically.

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>
@linear-code

linear-code Bot commented Nov 5, 2026

Copy link
Copy Markdown

OPE-97

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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 use expr.
  • 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

Comment thread pkg/shell/parser/parser.go Outdated
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] == '(' {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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>
@aakarim
aakarim merged commit a809fdc into main Nov 6, 2026
7 checks passed
@aakarim
aakarim deleted the fix/ope-97-reject-arithmetic-expansion branch November 6, 2026 14:53
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.

2 participants