From 35addc47c3fe56398ef16458c2c09b763e24ada9 Mon Sep 17 00:00:00 2001 From: Andy Grover Date: Sun, 18 Mar 2012 21:49:07 -0700 Subject: [PATCH] add proper error handling to loaded_modules() We need to check the result of basically all Py* calls and cleanup properly if they fail. Signed-off-by: Andy Grover --- libkmod.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/libkmod.c b/libkmod.c index b361335..dd82d9e 100644 --- a/libkmod.c +++ b/libkmod.c @@ -97,15 +97,32 @@ kmod_obj_loaded_modules(PyObject *self, PyObject *unused_args) } PyObject *pylist = PyList_New(0); + if (!pylist) { + kmod_module_unref_list(list); + return PyErr_NoMemory(); + } + /* refcountapallooza. */ kmod_list_foreach(itr, list) { struct kmod_module *mod = kmod_module_get_module(itr); const char *name = kmod_module_get_name(mod); long size = kmod_module_get_size(mod); PyObject *entry = Py_BuildValue("(sl)", name, size); - - PyList_Append(pylist, entry); + if (!entry) { + Py_DECREF(pylist); + kmod_module_unref(mod); + kmod_module_unref_list(list); + return NULL; + } + + if (PyList_Append(pylist, entry) == -1) { + Py_DECREF(entry); + Py_DECREF(pylist); + kmod_module_unref(mod); + kmod_module_unref_list(list); + return NULL; + } Py_DECREF(entry); kmod_module_unref(mod); -- 2.26.2