1
0
Fork 0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-06-11 02:46:05 +02:00

Fix ref counting on non-string dict keys

For bytes, there was an extraneous INCREF; PyIter_Next returns a new reference. For other non-strings, the original itemName before converting to a string was never dereferenced.

Fixes #419
This commit is contained in:
JustAnotherArchivist 2022-04-07 04:31:47 +00:00
parent f6860f1f3d
commit f4739140e3
2 changed files with 4 additions and 6 deletions

View File

@ -254,15 +254,13 @@ static int Dict_iterNext(JSOBJ obj, JSONTypeContext *tc)
return 1;
}
itemNameTmp = GET_TC(tc)->itemName;
GET_TC(tc)->itemName = PyObject_Str(GET_TC(tc)->itemName);
Py_DECREF(itemNameTmp);
itemNameTmp = GET_TC(tc)->itemName;
GET_TC(tc)->itemName = PyUnicode_AsUTF8String (GET_TC(tc)->itemName);
Py_DECREF(itemNameTmp);
}
else
{
Py_INCREF(GET_TC(tc)->itemName);
}
PRINTMARK();
return 1;
}

View File

@ -239,11 +239,11 @@ def test_encode_dict_values_ref_counting():
@pytest.mark.skipif(
hasattr(sys, "pypy_version_info"), reason="PyPy uses incompatible GC"
)
def test_encode_dict_key_ref_counting():
@pytest.mark.parametrize("key", ["key", b"key", 1, True, None])
def test_encode_dict_key_ref_counting(key):
import gc
gc.collect()
key = "key"
data = {key: "abc"}
ref_count = sys.getrefcount(key)
ujson.dumps(data)