mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-18 05:06:21 +00:00
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.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user