Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -11649,6 +11649,7 @@ ms_post_decode_float(
*************************************************************************/

#define ONE_E18 1000000000000000000ULL
#define ONE_E19_MINUS_ONE 9999999999999999999ULL

static MS_NOINLINE PyObject *
parse_number_fallback(
Expand Down Expand Up @@ -11948,7 +11949,7 @@ parse_number_inline(
(is_float) ||
((integer_end - integer_start) != 20) ||
(*integer_start != '1') ||
(mantissa <= ONE_E18)
(mantissa <= ONE_E19_MINUS_ONE)
)
) {
/* We overflowed. Redo parsing, truncating at 19 digits */
Expand Down
33 changes: 23 additions & 10 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,17 +1113,30 @@ def test_decode_int_boundaries(self, x):
assert isinstance(x2, int)
assert x2 == x

@pytest.mark.parametrize("x", [-(2**63) - 1, 2**64])
def test_decode_big_int_as_any(self, x):
s = str(x).encode()
x2 = msgspec.json.decode(s)
assert isinstance(x2, int)
assert x2 == x

@pytest.mark.parametrize("x", [-(2**63) - 1, 2**64])
def test_decode_big_int_as_int(self, x):
@pytest.mark.parametrize(
"x",
[
-(2**63) - 1,
2**64,
2**64 + 10**18,
2**64 + 10**18 + 1,
19999999999999999999,
20000000000000000000,
2**64 + 10**19, # mantissa overflows to 20 digits
2**64 + 10**19 - 1,
-(2**64),
-(2**64) + 10**18,
-(2**64) + 10**18 + 1,
-19999999999999999999,
-20000000000000000000,
-(2**64 + 10**19),
-(2**64 + 10**19 - 1),
],
)
@pytest.mark.parametrize("type", [Any, int])
def test_decode_big_int(self, x, type):
s = str(x).encode()
x2 = msgspec.json.decode(s, type=int)
x2 = msgspec.json.decode(s, type=type)
assert isinstance(x2, int)
assert x2 == x

Expand Down
Loading