summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Maximets <i.maximets@ovn.org>2022-07-22 21:16:42 +0200
committerIlya Maximets <i.maximets@ovn.org>2022-07-29 17:16:49 +0200
commitb63bbf2dba1a731f8de8a72a6e871acda770df0f (patch)
treeccc29d8e8ac7bd3a503b395c393abfa5b611a68c
parent4ad02ad047312c814b512cb8ab80a1dea372dbb9 (diff)
downloadopenvswitch-b63bbf2dba1a731f8de8a72a6e871acda770df0f.tar.gz
python-c-ext: Handle initialization failures.
PyModule_AddObject() may fail and it doesn't steal references in this case. The error condition should be handled to avoid possible memory leaks. And while it's not strictly specified if PyModule_Create may fail, most of the examples in python documentation include handling of a NULL case. Acked-by: Mike Pattrick <mkp@redhat.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
-rw-r--r--python/ovs/_json.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/python/ovs/_json.c b/python/ovs/_json.c
index 0b980038b..c36a140a8 100644
--- a/python/ovs/_json.c
+++ b/python/ovs/_json.c
@@ -229,9 +229,17 @@ PyInit__json(void)
if (PyType_Ready(&json_ParserType) < 0) {
return NULL;
}
+
m = PyModule_Create(&moduledef);
+ if (!m) {
+ return NULL;
+ }
Py_INCREF(&json_ParserType);
- PyModule_AddObject(m, "Parser", (PyObject *) & json_ParserType);
+ if (PyModule_AddObject(m, "Parser", (PyObject *) &json_ParserType) < 0) {
+ Py_DECREF(&json_ParserType);
+ Py_DECREF(m);
+ return NULL;
+ }
return m;
}