Skip to content

Commit 9ebe33c

Browse files
committed
Improve tests on int overflow
1 parent 8cff8a8 commit 9ebe33c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

decode_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"math"
2525
"reflect"
26+
"strconv"
2627
"strings"
2728
"testing"
2829
"time"
@@ -38,6 +39,16 @@ var negativeZero = math.Copysign(0.0, -1.0)
3839

3940
var unmarshalIntTest = 123
4041

42+
// archSafeInt returns an int64 representation of v that is safe for the current architecture.
43+
func archSafeInt(v int64) any {
44+
if strconv.IntSize == 64 || math.MinInt32 < v && v < math.MaxInt32 {
45+
return int(v) // int is safe
46+
}
47+
48+
// on 32-bit systems, and v is overflows int, we need to return an int64
49+
return int64(v)
50+
}
51+
4152
var unmarshalTests = []struct {
4253
data string
4354
value any
@@ -244,7 +255,7 @@ var unmarshalTests = []struct {
244255
},
245256
{
246257
"bin: -0b1000000000000000000000000000000000000000000000000000000000000000",
247-
map[string]int64{"bin": -9223372036854775808},
258+
map[string]any{"bin": archSafeInt(-9223372036854775808)},
248259
},
249260
{
250261
"decimal: +685_230",

0 commit comments

Comments
 (0)