mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.1k stars 336 forks source link

interp: panic on division by zero #892

Closed dankegel closed 1 year ago

dankegel commented 2 years ago

Given the invalid command

let x/x

bash rightfully complains:

$ let x/x
bash: let: x/x: division by 0 (error token is "x")

but gosh panics:

$ go run cmd/gosh/main.go
$ let x/x
panic: runtime error: integer divide by zero

goroutine 1 [running]:
mvdan.cc/sh/v3/expand.binArit(0x111540?, 0x589de0?, 0xc000122f28?)
    /home/dank/src/sh/expand/arith.go:173 +0x245
mvdan.cc/sh/v3/expand.Arithm(0x0?, {0x589840?, 0xc000078480?})
    /home/dank/src/sh/expand/arith.go:95 +0x353
...

This happens in v3.0.0 and master. I didn't try any tags older than v3.0.0. Tested with go 1.18.1.

mvdan commented 2 years ago

Thanks, this does look like a bug. Happy to accept a patch with a test.