Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exceptions on encoding list or dict elements and non-overflow errors on int handling getting silenced #505

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/ultrajsonenc.c
Expand Up @@ -656,6 +656,13 @@ 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.
endTypeContext must not be called here as beginTypeContext already cleans up in the INVALID case.
*/
SetError (obj, enc, "Invalid type");
enc->level--;
return;
}

Expand All @@ -681,6 +688,13 @@ 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)
{
enc->iterEnd(obj, &tc);
enc->endTypeContext(obj, &tc);
enc->level--;
return;
}
count ++;
}

Expand Down Expand Up @@ -725,6 +739,13 @@ 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)
{
enc->iterEnd(obj, &tc);
enc->endTypeContext(obj, &tc);
enc->level--;
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