From bb026cae89ad5bdd861d6ed8134a2e1b86cd3dc0 Mon Sep 17 00:00:00 2001 From: Haiyang Wang <30968392+ocean2811@users.noreply.github.com> Date: Wed, 23 Aug 2023 00:07:39 +0800 Subject: [PATCH] Decode: fix panic when parsing '0' as a float (#887) Fixes #886 --- decode.go | 2 +- unmarshaler_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/decode.go b/decode.go index 3a860d0..f0ec3b1 100644 --- a/decode.go +++ b/decode.go @@ -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") } diff --git a/unmarshaler_test.go b/unmarshaler_test.go index 4db58f7..c78ecb2 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -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`,