Skip to content

Commit 47cfd6a

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Fix 4 reference leaks in make_inline_cache_stats
Summary: PyUnicode_InternFromString and PyLong_FromLong return new references, but PyDict_SetItemString does not steal them. The new references were passed directly and never decref'd, leaking per cache entry. Wrap each in Ref<>::steal to ensure proper cleanup, matching the pattern already used for other allocations in the same function. Reviewed By: jbower-fb Differential Revision: D94681805 fbshipit-source-id: 2340ab6ccdfdfad913b96b83de4922311af71980
1 parent 9fc398c commit 47cfd6a

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

cinderx/Jit/pyjit.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,26 +2400,22 @@ PyObject* get_and_clear_inline_cache_stats(PyObject* /* self */, PyObject*) {
24002400

24012401
auto make_inline_cache_stats = [](PyObject* stats, CacheStats& cache_stats) {
24022402
auto result = Ref<>::steal(check(PyDict_New()));
2403-
check(PyDict_SetItemString(
2404-
result,
2405-
"filename",
2406-
PyUnicode_InternFromString(cache_stats.filename.c_str())));
2407-
check(PyDict_SetItemString(
2408-
result,
2409-
"method",
2410-
PyUnicode_InternFromString(cache_stats.method_name.c_str())));
2403+
auto filename = Ref<>::steal(
2404+
check(PyUnicode_InternFromString(cache_stats.filename.c_str())));
2405+
check(PyDict_SetItemString(result, "filename", filename));
2406+
auto method = Ref<>::steal(
2407+
check(PyUnicode_InternFromString(cache_stats.method_name.c_str())));
2408+
check(PyDict_SetItemString(result, "method", method));
24112409
auto cache_misses_dict = Ref<>::steal(check(PyDict_New()));
24122410
check(PyDict_SetItemString(result, "cache_misses", cache_misses_dict));
24132411
for (auto& [key, miss] : cache_stats.misses) {
24142412
auto py_key = Ref<>::steal(check(PyUnicode_FromString(key.c_str())));
24152413
auto miss_dict = Ref<>::steal(check(PyDict_New()));
2416-
check(PyDict_SetItemString(
2417-
miss_dict, "count", PyLong_FromLong(miss.count)));
2418-
check(PyDict_SetItemString(
2419-
miss_dict,
2420-
"reason",
2421-
PyUnicode_InternFromString(
2422-
std::string(cacheMissReason(miss.reason)).c_str())));
2414+
auto count = Ref<>::steal(check(PyLong_FromLong(miss.count)));
2415+
check(PyDict_SetItemString(miss_dict, "count", count));
2416+
auto reason = Ref<>::steal(check(PyUnicode_InternFromString(
2417+
std::string(cacheMissReason(miss.reason)).c_str())));
2418+
check(PyDict_SetItemString(miss_dict, "reason", reason));
24232419

24242420
check(PyDict_SetItem(cache_misses_dict, py_key, miss_dict));
24252421
}

0 commit comments

Comments
 (0)