From 77d3fce6e02e9a0828badf71001d8adf14d1fac8 Mon Sep 17 00:00:00 2001 From: Adil Karim Date: Thu, 5 Nov 2026 06:05:20 +0000 Subject: [PATCH 1/2] fix(shell): reject unsupported arithmetic expansion (OPE-97) 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 --- pkg/shell/cmds/shell_test.go | 51 ++++++++++++++++++++++++++++++++++++ pkg/shell/parser/lexer.go | 2 +- pkg/shell/parser/parser.go | 8 ++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/pkg/shell/cmds/shell_test.go b/pkg/shell/cmds/shell_test.go index ecdd7f6..9367c48 100644 --- a/pkg/shell/cmds/shell_test.go +++ b/pkg/shell/cmds/shell_test.go @@ -94,6 +94,57 @@ func TestVariableAssignment(t *testing.T) { } } +func TestArithmeticExpansionRejected(t *testing.T) { + for _, command := range []string{ + `echo $((2+3))`, + `x=$((2+3)); echo "x=[$x] exit=$?"`, + `L=5; echo "a=[$((L-1))] b=[$((2+3))]"`, + "sed -i \"$((2-1))a\\\nz\" /docs/arithmetic.txt", + `head -n $((5-1)) /docs/arithmetic.txt > /docs/arithmetic.txt`, + `echo changed > /docs/arithmetic.txt; echo $((2+3))`, + `echo $(echo $((2+3))) > /docs/arithmetic.txt`, + `echo ${UNSET:-$((2+3))} > /docs/arithmetic.txt`, + `for n in $((2+3)); do echo changed > /docs/arithmetic.txt; done`, + `echo changed > "$((2+3))"`, + `export N=$((2+3))`, + } { + t.Run(command, func(t *testing.T) { + fs := testFS() + const original = "a\nb\nc\n" + fs.AddFile("/docs/arithmetic.txt", original) + out, errOut, code := execCmd(t, fs, command) + if code == 0 || out != "" || !strings.Contains(errOut, "arithmetic expansion") || !strings.Contains(errOut, "use expr") { + t.Fatalf("code=%d stdout=%q stderr=%q, want arithmetic expansion error", code, out, errOut) + } + content, err := fs.ReadFile("/docs/arithmetic.txt") + if err != nil || string(content) != original { + t.Fatalf("file changed: content=%q err=%v", content, err) + } + }) + } +} + +func TestArithmeticExpansionLiteralAndCommandSubstitution(t *testing.T) { + for _, command := range []string{ + `echo '$((2+3))'`, + `echo "\$((2+3))"`, + `TEXT='$((2+3))'; echo "$TEXT"`, + "cat < Date: Fri, 6 Nov 2026 10:56:02 +0000 Subject: [PATCH 2/2] fix(shell): preserve unspaced subshell substitutions 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 --- pkg/shell/cmds/shell_test.go | 4 ++-- pkg/shell/parser/parser.go | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/shell/cmds/shell_test.go b/pkg/shell/cmds/shell_test.go index 9367c48..5995f81 100644 --- a/pkg/shell/cmds/shell_test.go +++ b/pkg/shell/cmds/shell_test.go @@ -139,8 +139,8 @@ func TestArithmeticExpansionLiteralAndCommandSubstitution(t *testing.T) { } // Command substitution (including a subshell) remains the supported way // to use expr's output as a word. - out, errOut, code := execCmd(t, testFS(), `echo $(expr 2 + 3); echo $( (expr 7 - 3) )`) - if code != 0 || errOut != "" || out != "5\n4\n" { + out, errOut, code := execCmd(t, testFS(), `echo $(expr 2 + 3); echo $( (expr 7 - 3) ); echo $((expr 9 - 2) ); echo "$((expr 8 - 5) )"`) + if code != 0 || errOut != "" || out != "5\n4\n7\n3\n" { t.Fatalf("expr substitution: code=%d stdout=%q stderr=%q", code, out, errOut) } } diff --git a/pkg/shell/parser/parser.go b/pkg/shell/parser/parser.go index 64c6032..8d83718 100644 --- a/pkg/shell/parser/parser.go +++ b/pkg/shell/parser/parser.go @@ -623,14 +623,6 @@ func (t *tokenizer) parseWordParts(s string, inDblQuote bool) []WordPart { if ch == '$' { 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] == '(' { - if t.err == nil { - t.err = fmt.Errorf("arithmetic expansion $((...)) is not supported; use expr") - } - return nil - } flushLit() // Command substitution i += 2 // skip $( @@ -669,6 +661,15 @@ func (t *tokenizer) parseWordParts(s string, inDblQuote bool) []WordPart { } i++ } + // $(( can also open a command substitution containing a + // subshell: $((expr 7 - 3) ). Only adjacent closing )) + // delimit arithmetic; an unclosed expansion is rejected too. + if start < len(s) && s[start] == '(' && (i >= len(s) || s[i-1] == ')') { + if t.err == nil { + t.err = fmt.Errorf("arithmetic expansion $((...)) is not supported; use expr") + } + return nil + } body := s[start:i] if i < len(s) { i++ // skip closing )