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..dea6d722 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):