Skip to content

Commit 4baeb95

Browse files
committed
Fix memory leak parsing large integers
1 parent 486bd45 commit 4baeb95

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/ujson/python/JSONtoObj.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ static JSOBJ Object_newIntegerFromString(void *prv, char *value, size_t length)
145145
char *buf = PyObject_Malloc(length + 1);
146146
memcpy(buf, value, length);
147147
buf[length] = '\0';
148-
return PyLong_FromString(buf, NULL, 10);
148+
PyObject *ret = PyLong_FromString(buf, NULL, 10);
149+
PyObject_Free(buf);
150+
return ret;
149151
}
150152

151153
static JSOBJ Object_newDouble(void *prv, double value)

tests/test_ujson.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,20 @@ def test_encode_decode_big_int(i, mode):
656656
assert ujson.decode(json_string) == python_object
657657

658658

659+
@pytest.mark.xfail(
660+
sys.implementation.name == "pypy",
661+
reason="PyPy's PyNumber_ToBase ignores sys.get_int_max_str_digits()",
662+
)
663+
def test_encode_too_big_int_error():
664+
with pytest.raises(ValueError, match="integer string conversion"):
665+
ujson.dumps(pow(10, 10_000))
666+
667+
668+
def test_decode_too_big_int_error():
669+
with pytest.raises(ValueError, match="integer string conversion"):
670+
ujson.loads("9" * 10_000)
671+
672+
659673
@pytest.mark.parametrize(
660674
"test_input, expected",
661675
[

0 commit comments

Comments
 (0)