Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pkg/shell/cmds/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF\n$((2+3))\nEOF",
`echo $(echo '$((2+3))')`,
} {
out, errOut, code := execCmd(t, testFS(), command)
if code != 0 || errOut != "" || out != "$((2+3))\n" {
t.Errorf("%s: code=%d stdout=%q stderr=%q, want literal arithmetic text", command, code, out, errOut)
}
}
// 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) ); 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)
}
}

func TestVariableDefault(t *testing.T) {
sh := shell.NewShell(testFS())
var out bytes.Buffer
Expand Down
2 changes: 1 addition & 1 deletion pkg/shell/parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ type tokenizer struct {
lex *lexer
peeked *token
prevVal string
err error // nested command-substitution parse failure
err error // word-expansion parse failure
}

func newTokenizer(src string) *tokenizer {
Expand Down
9 changes: 9 additions & 0 deletions pkg/shell/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,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 )
Expand Down
Loading