Browse files

Added ValueError exception to decoder when values are too big

  • Loading branch information...
1 parent ad25661 commit b7bdaa8cd799acd3f3a872c3fa938f8ee7565338 @jskorpan jskorpan committed Sep 24, 2012
Showing with 55 additions and 3 deletions.
  1. +11 −2 lib/ultrajsondec.c
  2. +1 −1 python/version.h
  3. +43 −0 tests/tests.py
View
13 lib/ultrajsondec.c
@@ -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;
View
2 python/version.h
@@ -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"
View
43 tests/tests.py
@@ -745,6 +745,49 @@ def test_decodeArrayOneItem(self):
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):

0 comments on commit b7bdaa8

Please sign in to comment.