From 099d1c7454477bbb0245ac1f5aed7c4061c65e91 Mon Sep 17 00:00:00 2001 From: Ilya Maximets Date: Fri, 22 Jul 2022 21:16:42 +0200 Subject: 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 Signed-off-by: Ilya Maximets --- python/ovs/_json.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'python') 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; } -- cgit v1.2.1