Skip to content

Commit

Permalink
Decode: fix panic when parsing '0' as a float (#887)
Browse files Browse the repository at this point in the history
Fixes #886
  • Loading branch information
ocean2811 authored Aug 22, 2023
1 parent f7d9b9b commit bb026ca
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func parseFloat(b []byte) (float64, error) {
if cleaned[0] == '+' || cleaned[0] == '-' {
start = 1
}
if cleaned[start] == '0' && isDigit(cleaned[start+1]) {
if cleaned[start] == '0' && len(cleaned) > start+1 && isDigit(cleaned[start+1]) {
return 0, unstable.NewParserError(b, "float integer part cannot have leading zeroes")
}

Expand Down
5 changes: 5 additions & 0 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ func TestUnmarshal_Floats(t *testing.T) {
input: `0E0`,
expected: 0.0,
},
{
desc: "float zero without decimals",
input: `0`,
expected: 0.0,
},
{
desc: "float fractional with exponent",
input: `6.626e-34`,
Expand Down

0 comments on commit bb026ca

Please sign in to comment.