1
0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-11-23 12:32:02 +01:00

Add checks to test suite and improve comments

This commit is contained in:
Mario Garcia-Armas 2022-10-14 17:52:37 +00:00
parent 4d82888289
commit 1161d5d27d
2 changed files with 11 additions and 3 deletions

@ -79,6 +79,8 @@ static JSOBJ SetError( struct DecoderState *ds, int offset, const char *message)
static FASTCALL_ATTR JSOBJ FASTCALL_MSVC decodeDouble(struct DecoderState *ds)
{
int processed_characters_count;
/* Prevent int overflow if ds->end - ds->start is too large. See check_decode_decimal_no_int_overflow()
inside tests/test_ujson.py for an example where this check is necessary. */
int len = ((size_t) (ds->end - ds->start) < (size_t) INT_MAX) ? (int) (ds->end - ds->start) : INT_MAX;
double value = dconv_s2d(ds->dec->s2d, ds->start, len, &processed_characters_count);
ds->lastType = JT_DOUBLE;

@ -1124,9 +1124,15 @@ def test_separators_errors(separators, expected_exception):
ujson.dumps({"a": 0, "b": 1}, separators=separators)
def test_decode_decimal_no_int_overflow():
# Takes a while because the string is large; feel free to comment out or remove
ujson.decode(r'[0.123456789,"{}"]'.format("a" * (2**32 - 5)))
"""
The following checks are not part of the standard test suite.
They can be run manually as follows:
python -c 'from tests.test_ujson import check_foo; check_foo()'
"""
def check_decode_decimal_no_int_overflow():
# Requires enough free RAM to hold a ~4GB string in memory
decoded = ujson.decode(r'[0.123456789,"{}"]'.format("a" * (2**32 - 5)))
assert decoded[0] == 0.123456789
"""