summaryrefslogtreecommitdiff
path: root/compat
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-17 19:21:37 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-25 00:47:52 -0700
commitd01fa91dcdfd428fb4b1c46b3961a1497c7a1102 (patch)
tree0724da76664e893e7ecfad2d8ce4ea1b05918598 /compat
parentf369aff688697a881833d86c13b18156e8376f08 (diff)
decode: Don't bail on type errors
Diffstat (limited to 'compat')
-rw-r--r--compat/json/compat_test.go8
1 files changed, 8 insertions, 0 deletions
diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go
index df9d387..098ac85 100644
--- a/compat/json/compat_test.go
+++ b/compat/json/compat_test.go
@@ -181,6 +181,10 @@ func TestCompatUnmarshal(t *testing.T) {
"two-objs": {In: `{} {}`, ExpOut: nil, ExpErr: `invalid character '{' after top-level value`},
"two-numbers1": {In: `00`, ExpOut: nil, ExpErr: `invalid character '0' after top-level value`},
"two-numbers2": {In: `1 2`, ExpOut: nil, ExpErr: `invalid character '2' after top-level value`},
+ // 2e308 is slightly more than math.MaxFloat64 (~1.79e308)
+ "obj-overflow": {In: `{"foo":"bar", "baz":2e308, "qux": "orb"}`, ExpOut: map[string]any{"foo": "bar", "baz": nil, "qux": "orb"}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
+ "ary-overflow": {In: `["foo",2e308,"bar",3e308]`, ExpOut: []any{"foo", nil, "bar", nil}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
+ "existing-overflow": {In: `2e308`, InPtr: func() any { x := 4; return &x }(), ExpOut: 4, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type int`},
}
for tcName, tc := range testcases {
tc := tc
@@ -219,6 +223,10 @@ func TestCompatDecode(t *testing.T) {
"two-objs": {In: `{} {}`, ExpOut: map[string]any{}},
"two-numbers1": {In: `00`, ExpOut: float64(0)},
"two-numbers2": {In: `1 2`, ExpOut: float64(1)},
+ // 2e308 is slightly more than math.MaxFloat64 (~1.79e308)
+ "obj-overflow": {In: `{"foo":"bar", "baz":2e308, "qux": "orb"}`, ExpOut: map[string]any{"foo": "bar", "baz": nil, "qux": "orb"}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
+ "ary-overflow": {In: `["foo",2e308,"bar",3e308]`, ExpOut: []any{"foo", nil, "bar", nil}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
+ "existing-overflow": {In: `2e308`, InPtr: func() any { x := 4; return &x }(), ExpOut: 4, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type int`},
}
for tcName, tc := range testcases {
tc := tc