From 59cd7d6f2933bf9bd9301be74d8d218c11985cd2 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Sat, 12 Feb 2022 17:59:34 +0000 Subject: [PATCH] Fix exceptions on encoding list or dict elements and non-overflow errors on int handling getting silenced Fixes #273 --- lib/ultrajsonenc.c | 13 +++++++++++++ python/objToJSON.c | 8 +++++++- tests/test_ujson.py | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/ultrajsonenc.c b/lib/ultrajsonenc.c index a9f3ef16..899cbac2 100644 --- a/lib/ultrajsonenc.c +++ b/lib/ultrajsonenc.c @@ -656,6 +656,11 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c { case JT_INVALID: { + /* + There should already be an exception at the Python level. + This however sets the errorMsg so recursion on arrays and objects stops. + */ + SetError (obj, enc, "Invalid type"); return; } @@ -681,6 +686,10 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c enc->level ++; Buffer_AppendIndentUnchecked (enc, enc->level); encode (iterObj, enc, NULL, 0); + if (enc->errorMsg) + { + return; + } count ++; } @@ -725,6 +734,10 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c enc->level ++; Buffer_AppendIndentUnchecked (enc, enc->level); encode (iterObj, enc, objName, szlen); + if (enc->errorMsg) + { + return; + } count ++; } diff --git a/python/objToJSON.c b/python/objToJSON.c index 3aae152a..dfeab3ce 100644 --- a/python/objToJSON.c +++ b/python/objToJSON.c @@ -505,12 +505,18 @@ static void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc, JSONObject GET_TC(tc)->unsignedLongValue = PyLong_AsUnsignedLongLong(obj); exc = PyErr_Occurred(); - if (exc && PyErr_ExceptionMatches(PyExc_OverflowError)) + if (exc) { PRINTMARK(); goto INVALID; } } + else + if (exc) + { + PRINTMARK(); + goto INVALID; + } return; } diff --git a/tests/test_ujson.py b/tests/test_ujson.py index 649629da..ed3c7dc5 100644 --- a/tests/test_ujson.py +++ b/tests/test_ujson.py @@ -659,6 +659,10 @@ def test_dumps_raises(test_input, expected_exception, expected_message): (float("inf"), OverflowError), (-float("inf"), OverflowError), (12839128391289382193812939, OverflowError), + ([12839128391289382193812939], OverflowError), + ([12839128391289382193812939, 42], OverflowError), + ({'a': 12839128391289382193812939}, OverflowError), + ({'a': 12839128391289382193812939, 'b': 42}, OverflowError), ], ) def test_encode_raises_allow_nan(test_input, expected_exception):