If PyModule_AddObject() failed, an exception was set, and we must
return. The code importing the module will fail with the set exception.
While fixing this issue, use more standard code for handling global
python objects:
- Always increase error reference, to make sure the gloabl does not
become dangling pointer if sanlock.SanlockException is modified by the
application.
- If PyModule_AddObject() fails, decrease the referene.
- If the module is reloaded, do not create a new SanlockException object
and leak the previous object.
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
python/sanlock.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/python/sanlock.c b/python/sanlock.c
index 2ad4497..a9c4cf6 100644
--- a/python/sanlock.c
+++ b/python/sanlock.c
@@ -1614,17 +1614,20 @@ initsanlock(void)
sanlock_methods, pydoc_sanlock, NULL, PYTHON_API_VERSION);
if (py_module == NULL)
return;
- py_exception = initexception();
+ if (py_exception == NULL) {
+ py_exception = initexception();
+ if (py_exception == NULL)
+ return;
+ }
- if (py_exception == NULL)
+ Py_INCREF(py_exception);
+ if (PyModule_AddObject(py_module, "SanlockException", py_exception)) {
+ Py_DECREF(py_exception);
return;
-
- if (PyModule_AddObject(py_module, "SanlockException", py_exception) == 0)
{
- Py_INCREF(py_exception);
}
#define PYSNLK_INIT_ADD_CONSTANT(x, y) \
if ((sk_constant = PyInt_FromLong(x)) != NULL) { \
if (PyModule_AddObject(py_module, y, sk_constant)) { \
--
2.17.2
Show replies by date