From 6871a4d46fea2df7fd397a67499f72ddde639fc8 Mon Sep 17 00:00:00 2001 From: Jabber Developer Date: Wed, 2 Jul 2025 15:05:44 +0200 Subject: [PATCH] Fix memory leak in _handle_string_or_none_result The function did not decref the Python object on type error paths, resulting in a reference count leak. This caused memory to accumulate over time. Added Py_XDECREF on all early return paths to properly free the Python object reference and prevent leaks. --- src/plugins/python_plugins.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index 8cd721df..13278750 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -900,6 +900,8 @@ _python_type_error(ProfPlugin* plugin, char* hook, char* type) g_string_free(err_msg, TRUE); } +// Converts a Python string or None result to a C string and decreases the Python object's reference count. +// If the result is NULL, or not a string/unicode/bytes/None, logs an error and returns NULL. static char* _handle_string_or_none_result(ProfPlugin* plugin, PyObject* result, char* hook) { @@ -910,18 +912,16 @@ _handle_string_or_none_result(ProfPlugin* plugin, PyObject* result, char* hook) } #ifdef PY_IS_PYTHON3 if (result != Py_None && !PyUnicode_Check(result) && !PyBytes_Check(result)) { - allow_python_threads(); - _python_type_error(plugin, hook, "string, unicode or None"); - return NULL; - } #else if (result != Py_None && !PyUnicode_Check(result) && !PyString_Check(result)) { +#endif + Py_XDECREF(result); allow_python_threads(); _python_type_error(plugin, hook, "string, unicode or None"); return NULL; } -#endif char* result_str = python_str_or_unicode_to_string(result); + Py_XDECREF(result); allow_python_threads(); return result_str; }