bwmarrin / snowflake

A simple to use Go (golang) package to generate or parse Twitter snowflake IDs
BSD 2-Clause "Simplified" License
3.02k stars 374 forks source link

Fix panic during UnmarshalJSON #8

Closed connor4312 closed 7 years ago

connor4312 commented 7 years ago

Previously, this would panic with the following if a single-character value like 1 was provided. Instead, return a nice error.

=== RUN   TestUnmarshalJSON
--- FAIL: TestUnmarshalJSON (0.00s)
panic: runtime error: slice bounds out of range [recovered]
    panic: runtime error: slice bounds out of range

goroutine 20 [running]:
testing.tRunner.func1(0xc42007c750)
    /usr/local/Cellar/go/1.8_1/libexec/src/testing/testing.go:622 +0x29d
panic(0x1113860, 0x11cc720)
    /usr/local/Cellar/go/1.8_1/libexec/src/runtime/panic.go:489 +0x2cf
github.com/bwmarrin/snowflake.(*ID).UnmarshalJSON(0xc420028e68, 0xc420028e80, 0x1, 0x20, 0x1, 0x20)
    /Users/copeet/go/src/github.com/bwmarrin/snowflake/snowflake.go:203 +0xf9
github.com/bwmarrin/snowflake.TestUnmarshalJSON(0xc42007c750)
    /Users/copeet/go/src/github.com/bwmarrin/snowflake/snowflake_test.go:42 +0x15c
testing.tRunner(0xc42007c750, 0x113f2d0)
    /usr/local/Cellar/go/1.8_1/libexec/src/testing/testing.go:657 +0x96
created by testing.(*T).Run
    /usr/local/Cellar/go/1.8_1/libexec/src/testing/testing.go:697 +0x2ca
exit status 2
FAIL    github.com/bwmarrin/snowflake   0.008s
bwmarrin commented 7 years ago

Oh! Thanks Connor! Sorry I just now saw this! I'm bad at github.