Skip to content

Commit

Permalink
Fix segmentation fault when an exception is raised while converting a…
Browse files Browse the repository at this point in the history
… dict key to a string

Fixes #522
  • Loading branch information
JustAnotherArchivist committed Apr 13, 2022
1 parent 62dec8d commit 935fe0c
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 @@ -996,6 +996,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 935fe0c

Please sign in to comment.