From b773bf05dc59474327822618fc836ee73a9d262f Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Wed, 11 Nov 2020 14:51:27 +0100 Subject: [PATCH] Fix errors on reading long decimal floats --- lib/ultrajsondec.c | 16 ++++++++++++++-- tests/test_ujson.py | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/ultrajsondec.c b/lib/ultrajsondec.c index ec73326..184b5b9 100644 --- a/lib/ultrajsondec.c +++ b/lib/ultrajsondec.c @@ -90,6 +90,7 @@ static FASTCALL_ATTR JSOBJ FASTCALL_MSVC decodeDouble(struct DecoderState *ds) static FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds) { int intNeg = 1; + int hasError = 0; JSUINT64 intValue; JSUINT64 prevIntValue; int chr; @@ -130,11 +131,11 @@ static FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds if (intNeg == 1 && prevIntValue > intValue) { - return SetError(ds, -1, "Value is too big!"); + hasError = 1; } else if (intNeg == -1 && intValue > overflowLimit) { - return SetError(ds, -1, overflowLimit == LLONG_MAX ? "Value is too big!" : "Value is too small"); + hasError = 1; } offset ++; @@ -154,6 +155,17 @@ static FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds default: { + if (hasError) + { + if (intNeg == 1) + { + return SetError(ds, -1, "Value is too big!"); + } + else if (intNeg == -1) + { + return SetError(ds, -1, overflowLimit == LLONG_MAX ? "Value is too big!" : "Value is too small"); + } + } goto BREAK_INT_LOOP; break; } diff --git a/tests/test_ujson.py b/tests/test_ujson.py index b11e5d2..fb95f3c 100644 --- a/tests/test_ujson.py +++ b/tests/test_ujson.py @@ -533,6 +533,10 @@ def test_decode_numeric_int_exp(test_input): assert output == json.loads(test_input) +def test_decode_long_float(): + assert ujson.decode("100000000000000000000.0") == 1e20 + + @pytest.mark.parametrize( "test_input, expected", [