From dee0375ffd4412e1883a406de0e3b315cdc7ff13 Mon Sep 17 00:00:00 2001 From: tidwall Date: Sun, 28 Mar 2021 18:30:25 -0700 Subject: [PATCH] Fix negative without int part for valid fixes #210 --- gjson.go | 6 ++++++ gjson_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/gjson.go b/gjson.go index 148c88e..7bb82e9 100644 --- a/gjson.go +++ b/gjson.go @@ -2382,6 +2382,12 @@ func validnumber(data []byte, i int) (outi int, ok bool) { // sign if data[i] == '-' { i++ + if i == len(data) { + return i, false + } + if data[i] < '0' || data[i] > '9' { + return i, false + } } // int if i == len(data) { diff --git a/gjson_test.go b/gjson_test.go index 6f4dd7a..a1b939f 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -1030,6 +1030,7 @@ func TestValidBasic(t *testing.T) { testvalid(t, "00", false) testvalid(t, "-00", false) testvalid(t, "-.", false) + testvalid(t, "-.123", false) testvalid(t, "0.0", true) testvalid(t, "10.0", true) testvalid(t, "10e1", true) @@ -1094,6 +1095,8 @@ func TestValidBasic(t *testing.T) { testvalid(t, `"a\\b\\\uFFA"`, false) testvalid(t, string(complicatedJSON), true) testvalid(t, string(exampleJSON), true) + testvalid(t, "[-]", false) + testvalid(t, "[-.123]", false) } var jsonchars = []string{"{", "[", ",", ":", "}", "]", "1", "0", "true",