]> git.feebdaed.xyz Git - 0xmirror/go.git/commitdiff
json/jsontext: normalize all invalid Kinds to 0
authorDamien Neil <dneil@google.com>
Mon, 8 Dec 2025 21:54:15 +0000 (13:54 -0800)
committerGopher Robot <gobot@golang.org>
Thu, 11 Dec 2025 17:35:35 +0000 (09:35 -0800)
For #75431

Change-Id: Iafefe952d3c1837e2f4c8c24cae96945d9e5abbf
Reviewed-on: https://go-review.googlesource.com/c/go/+/728380
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Auto-Submit: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/encoding/json/jsontext/decode_test.go
src/encoding/json/jsontext/token.go

index 209ff65ec8191bbdd6fd51ccb9fd33f644015210..3f48cae2d1d09377e0c6697b3b098dee41361efd 100644 (file)
@@ -193,8 +193,8 @@ var decoderErrorTestdata = []struct {
        name: jsontest.Name("InvalidStart"),
        in:   ` #`,
        calls: []decoderMethodCall{
-               {'#', zeroToken, newInvalidCharacterError("#", "at start of value").withPos(" ", ""), ""},
-               {'#', zeroValue, newInvalidCharacterError("#", "at start of value").withPos(" ", ""), ""},
+               {0, zeroToken, newInvalidCharacterError("#", "at start of value").withPos(" ", ""), ""},
+               {0, zeroValue, newInvalidCharacterError("#", "at start of value").withPos(" ", ""), ""},
        },
 }, {
        name: jsontest.Name("StreamN0"),
index e78c3f84d8650fec92a739f7d7016f4b881447c4..caf01749237413035db7968edf160f5c6a970286 100644 (file)
@@ -518,10 +518,31 @@ func (k Kind) String() string {
        }
 }
 
-// normalize coalesces all possible starting characters of a number as just '0'.
+var normKind = [256]Kind{
+       'n': 'n',
+       'f': 'f',
+       't': 't',
+       '"': '"',
+       '{': '{',
+       '}': '}',
+       '[': '[',
+       ']': ']',
+       '-': '0',
+       '0': '0',
+       '1': '0',
+       '2': '0',
+       '3': '0',
+       '4': '0',
+       '5': '0',
+       '6': '0',
+       '7': '0',
+       '8': '0',
+       '9': '0',
+}
+
+// normalize coalesces all possible starting characters of a number as just '0',
+// and converts all invalid kinds to 0.
 func (k Kind) normalize() Kind {
-       if k == '-' || ('0' <= k && k <= '9') {
-               return '0'
-       }
-       return k
+       // A lookup table keeps the inlining cost as low as possible.
+       return normKind[k]
 }