2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-19 14:37:21 +00:00

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>
This commit is contained in:
Ilya Maximets
2022-07-22 21:16:42 +02:00
parent 318adf3f33
commit 099d1c7454

View File

@@ -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;
}