mirror of
https://github.com/ultrajson/ultrajson.git
synced 2024-06-10 10:06:03 +02:00
Added ValueError exception to decoder when values are too big
This commit is contained in:
parent
ad25661f95
commit
b7bdaa8cd7
|
@ -80,7 +80,7 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric ( struct DecoderState *ds)
|
|||
double intValue;
|
||||
#else
|
||||
int intNeg = 1;
|
||||
JSLONG intValue;
|
||||
JSUINT64 intValue;
|
||||
#endif
|
||||
|
||||
double expNeg;
|
||||
|
@ -90,10 +90,13 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric ( struct DecoderState *ds)
|
|||
double expValue;
|
||||
char *offset = ds->start;
|
||||
|
||||
JSUINT64 overflowLimit = LLONG_MAX;
|
||||
|
||||
if (*(offset) == '-')
|
||||
{
|
||||
offset ++;
|
||||
intNeg = -1;
|
||||
overflowLimit = LLONG_MIN;
|
||||
}
|
||||
|
||||
// Scan integer part
|
||||
|
@ -120,8 +123,14 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric ( struct DecoderState *ds)
|
|||
#ifdef JSON_DECODE_NUMERIC_AS_DOUBLE
|
||||
intValue = intValue * 10.0 + (double) (chr - 48);
|
||||
#else
|
||||
intValue = intValue * 10LL + (JSLONG) (chr - 48);
|
||||
intValue = intValue * 10ULL + (JSLONG) (chr - 48);
|
||||
|
||||
if (intValue > overflowLimit)
|
||||
{
|
||||
return SetError(ds, -1, overflowLimit == LLONG_MAX ? "Value is too big" : "Value is too small");
|
||||
}
|
||||
#endif
|
||||
|
||||
offset ++;
|
||||
break;
|
||||
|
||||
|
|
|
@ -31,4 +31,4 @@ Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights rese
|
|||
|
||||
*/
|
||||
|
||||
#define UJSON_VERSION "1.20"
|
||||
#define UJSON_VERSION "1.21"
|
||||
|
|
|
@ -745,6 +745,49 @@ class UltraJSONTests(TestCase):
|
|||
input = "[31337]"
|
||||
ujson.decode(input)
|
||||
|
||||
def test_decodeBigValue(self):
|
||||
input = "9223372036854775807"
|
||||
ujson.decode(input)
|
||||
|
||||
def test_decodeSmallValue(self):
|
||||
input = "-9223372036854775808"
|
||||
ujson.decode(input)
|
||||
|
||||
def test_decodeTooBigValue(self):
|
||||
try:
|
||||
input = "9223372036854775808"
|
||||
ujson.decode(input)
|
||||
except ValueError, e:
|
||||
pass
|
||||
else:
|
||||
assert False, "expected ValueError"
|
||||
|
||||
def test_decodeTooSmallValue(self):
|
||||
try:
|
||||
input = "-90223372036854775809"
|
||||
ujson.decode(input)
|
||||
except ValueError,e:
|
||||
pass
|
||||
else:
|
||||
assert False, "expected ValueError"
|
||||
|
||||
def test_decodeVeryTooBigValue(self):
|
||||
try:
|
||||
input = "9223372036854775808"
|
||||
ujson.decode(input)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
assert False, "expected ValueError"
|
||||
|
||||
def test_decodeVeryTooSmallValue(self):
|
||||
try:
|
||||
input = "-90223372036854775809"
|
||||
ujson.decode(input)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
assert False, "expected ValueError"
|
||||
|
||||
"""
|
||||
def test_decodeNumericIntFrcOverflow(self):
|
||||
|
|
Loading…
Reference in New Issue