Skip to content

Commit

Permalink
Fix exceptions on encoding list or dict elements and non-overflow err…
Browse files Browse the repository at this point in the history
…ors on int handling getting silenced

Fixes #273
  • Loading branch information
JustAnotherArchivist committed Feb 12, 2022
1 parent c41db2a commit 80e1aee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/ultrajsonenc.c
Expand Up @@ -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;
}

Expand All @@ -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 ++;
}

Expand Down Expand Up @@ -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 ++;
}

Expand Down
8 changes: 7 additions & 1 deletion python/objToJSON.c
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/test_ujson.py
Expand Up @@ -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):
Expand Down

0 comments on commit 80e1aee

Please sign in to comment.