justinian/dice

`panic: runtime error: slice bounds out of range [:1] with capacity 0` with input `0d1d01`

Closed this issue · 2 comments

Here's a minimal reproduction of the issue:

package main

import (
	"fmt"

	"github.com/justinian/dice"
)

func main() {
	_, output, err := dice.Roll("0d1d01")
	fmt.Println("Output:", output)
	fmt.Println("Error:", err)
}

Playground link: https://go.dev/play/p/nGJLTC3dV_T

And the full output:

panic: runtime error: slice bounds out of range [:1] with capacity 0

goroutine 1 [running]:
github.com/justinian/dice.StdRoller.Roll({}, {0xc000116400, 0x8, 0xc00006a180?})
	/tmp/gopath3705264433/pkg/mod/github.com/justinian/dice@v1.0.2/std.go:90 +0x515
github.com/justinian/dice.Roll({0x4e1269, 0x6})
	/tmp/gopath3705264433/pkg/mod/github.com/justinian/dice@v1.0.2/dice.go:74 +0xd1
main.main()
	/tmp/sandbox2393286976/prog.go:10 +0x25

Program exited.

I'm aware that the input is an invalid format, but I would expect an error message rather than a panic.

In case you're wondering, I was playing around with fuzz testing and that's how I encountered the error. I'm happy to contribute a fuzz test to the library if you'd like :)

I've not done much with fuzz testing, especially in Go. I'd love a contribution, for sure.