Skip to content

Commit

Permalink
Merge pull request #526 from JustAnotherArchivist/fix-obj-str-excepti…
Browse files Browse the repository at this point in the history
…on-segv

Fix segmentation fault when an exception is raised while converting a dict key to a string
  • Loading branch information
hugovk committed Apr 14, 2022
2 parents b8a85c8 + 935fe0c commit 7799498
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/objToJSON.c
Expand Up @@ -257,6 +257,11 @@ static int Dict_iterNext(JSOBJ obj, JSONTypeContext *tc)
itemNameTmp = GET_TC(tc)->itemName;
GET_TC(tc)->itemName = PyObject_Str(GET_TC(tc)->itemName);
Py_DECREF(itemNameTmp);
if (PyErr_Occurred())
{
PRINTMARK();
return -1;
}
itemNameTmp = GET_TC(tc)->itemName;
GET_TC(tc)->itemName = PyUnicode_AsUTF8String (GET_TC(tc)->itemName);
Py_DECREF(itemNameTmp);
Expand Down Expand Up @@ -332,6 +337,10 @@ static int SortedDict_iterNext(JSOBJ obj, JSONTypeContext *tc)
else if (!PyBytes_Check(key))
{
key = PyObject_Str(key);
if (PyErr_Occurred())
{
goto error;
}
keyTmp = key;
key = PyUnicode_AsUTF8String(key);
Py_DECREF(keyTmp);
Expand Down
10 changes: 10 additions & 0 deletions tests/test_ujson.py
Expand Up @@ -997,6 +997,16 @@ def convert(self):
)


@pytest.mark.parametrize("sort_keys", [False, True])
def test_obj_str_exception(sort_keys):
class Obj:
def __str__(self):
raise NotImplementedError

with pytest.raises(NotImplementedError):
ujson.dumps({Obj(): 1}, sort_keys=sort_keys)


"""
def test_decode_numeric_int_frc_overflow():
input = "X.Y"
Expand Down

0 comments on commit 7799498

Please sign in to comment.