gplugin/gplugin

Fix various leaks in Python loader (hopefully).
develop/asan
2019-02-01, Elliott Sales de Andrade
3418e24e0842
Fix various leaks in Python loader (hopefully).

These were mostly done by hand, based on the new/borrowed reference annotation
in the docs. Unfortunately, since we never deinitialize the interpreter, asan
still says most of these things leak.
--- a/python/gplugin-python-loader.c Thu Jan 31 22:12:25 2019 -0500
+++ b/python/gplugin-python-loader.c Fri Feb 01 00:22:56 2019 -0500
@@ -50,7 +50,7 @@
}
static GPluginPlugin *
-gplugin_python_loader_query(G_GNUC_UNUSED GPluginLoader *loader,
+gplugin_python_loader_query(GPluginLoader *loader,
const gchar *filename,
G_GNUC_UNUSED GError **error)
{
@@ -82,6 +82,10 @@
g_warning(_("Failed to query %s"), filename);
PyErr_Print();
+ /* clean some stuff up */
+ g_free(module_name);
+ Py_DECREF(package_list);
+
pyg_gil_state_release(state);
return NULL;
@@ -101,6 +105,7 @@
g_warning(_("Failed to find the gplugin_query function in %s"),
filename);
+ Py_DECREF(module);
pyg_gil_state_release(state);
return NULL;
@@ -110,6 +115,7 @@
"function"),
filename);
+ Py_DECREF(module);
pyg_gil_state_release(state);
return NULL;
@@ -120,6 +126,7 @@
g_warning(_("Failed to find the gplugin_load function in %s"),
filename);
+ Py_DECREF(module);
pyg_gil_state_release(state);
return NULL;
@@ -129,6 +136,7 @@
"function"),
filename);
+ Py_DECREF(module);
pyg_gil_state_release(state);
return NULL;
@@ -139,6 +147,7 @@
g_warning(_("Failed to find the gplugin_unload function in %s"),
filename);
+ Py_DECREF(module);
pyg_gil_state_release(state);
return NULL;
@@ -148,6 +157,7 @@
"function"),
filename);
+ Py_DECREF(module);
pyg_gil_state_release(state);
return NULL;
@@ -172,6 +182,9 @@
"unload-func", unload,
NULL);
+ Py_DECREF(pyinfo);
+ Py_DECREF(module);
+
/* unlock the gil */
pyg_gil_state_release(state);
@@ -193,10 +206,14 @@
result = PyObject_CallFunctionObjArgs(load, pyplugin, NULL);
Py_DECREF(pyplugin);
- if(error) {
- *error = gplugin_python_exception_to_gerror();
- if(*error)
- return FALSE;
+ if (PyErr_Occurred()) {
+ Py_XDECREF(result);
+
+ if (error) {
+ *error = gplugin_python_exception_to_gerror();
+ }
+
+ return FALSE;
}
ret = PyObject_IsTrue(result);
@@ -228,7 +245,7 @@
if(PyErr_Occurred()) {
PyErr_Print();
- Py_DECREF(result);
+ Py_XDECREF(result);
return FALSE;
}
@@ -255,6 +272,7 @@
PyErr_Fetch(&type, &value, &tb);
Py_DECREF(type);
+ Py_XDECREF(tb);
obj = PyUnicode_AsUTF8String(value);
Py_DECREF(value);
@@ -277,7 +295,7 @@
static gboolean
gplugin_python_loader_init_gettext(void) {
PyObject *module_dict = NULL, *install = NULL;
- PyObject *gettext = NULL, *gettext_args = NULL;
+ PyObject *gettext = NULL, *result = NULL;
gettext = PyImport_ImportModule("gettext");
if(gettext == NULL) {
@@ -288,9 +306,9 @@
module_dict = PyModule_GetDict(gettext);
install = PyDict_GetItemString(module_dict, "install");
- gettext_args = Py_BuildValue("ss", GETTEXT_PACKAGE, LOCALEDIR);
- PyObject_CallObject(install, gettext_args);
- Py_DECREF(gettext_args);
+ result = PyObject_CallFunction(install, "ss", GETTEXT_PACKAGE, LOCALEDIR);
+ Py_XDECREF(result);
+ Py_DECREF(gettext);
return TRUE;
}
--- a/python/gplugin-python-test-pygobject.c Thu Jan 31 22:12:25 2019 -0500
+++ b/python/gplugin-python-test-pygobject.c Fri Feb 01 00:22:56 2019 -0500
@@ -39,8 +39,10 @@
wargv[0] = g_new0(wchar_t, len + 1);
len = mbstowcs(wargv[0], argv[0], len + 1);
- if(len == (size_t)-1)
+ if(len == (size_t)-1) {
+ g_free(wargv[0]);
return -1;
+ }
/* setup sys.path */
#if PY_VERSION_HEX < 0x03010300