Skip to content

Commit 32ac7d4

Browse files
committed
sudo_module_register_loghandler: clear sudo_type_LogHandler on error
Also add comments about PyModule_AddObject stealing a ref on success. --HG-- branch : 1.9
1 parent 2b0643f commit 32ac7d4

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

plugins/python/python_baseplugin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ sudo_module_register_baseplugin(PyObject *py_module)
7474
goto cleanup;
7575
}
7676

77+
// PyModule_AddObject steals a reference to py_class on success
7778
Py_INCREF(py_class);
7879
rc = SUDO_RC_OK;
7980

plugins/python/python_convmessage.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ sudo_module_register_conv_message(PyObject *py_module)
9191
goto cleanup;
9292
}
9393

94+
// PyModule_AddObject steals the reference to py_class on success
9495
Py_INCREF(py_class);
9596
rc = SUDO_RC_OK;
9697

plugins/python/python_loghandler.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ sudo_module_register_loghandler(PyObject *py_module)
148148
if (sudo_type_LogHandler == NULL)
149149
goto cleanup;
150150

151-
if (PyModule_AddObject(py_module, "LogHandler", sudo_type_LogHandler) < 0)
151+
if (PyModule_AddObject(py_module, "LogHandler", sudo_type_LogHandler) < 0) {
152+
Py_CLEAR(sudo_type_LogHandler);
152153
goto cleanup;
154+
}
153155

156+
// PyModule_AddObject steals a reference to sudo_type_LogHandler on success
154157
Py_INCREF(sudo_type_LogHandler);
155158

156159
cleanup:

plugins/python/sudo_python_module.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -479,28 +479,26 @@ sudo_module_register_enum(PyObject *py_module, const char *enum_name, PyObject *
479479
return;
480480

481481
PyObject *py_enum_class = NULL;
482-
{
483-
PyObject *py_enum_module = PyImport_ImportModule("enum");
484-
if (py_enum_module == NULL) {
485-
Py_CLEAR(py_constants_dict);
486-
debug_return;
487-
}
482+
PyObject *py_enum_module = PyImport_ImportModule("enum");
483+
if (py_enum_module == NULL) {
484+
Py_CLEAR(py_constants_dict);
485+
debug_return;
486+
}
488487

489-
py_enum_class = PyObject_CallMethod(py_enum_module,
490-
"IntEnum", "sO", enum_name,
491-
py_constants_dict);
488+
py_enum_class = PyObject_CallMethod(py_enum_module,
489+
"IntEnum", "sO", enum_name,
490+
py_constants_dict);
492491

493-
Py_CLEAR(py_constants_dict);
494-
Py_CLEAR(py_enum_module);
495-
}
492+
Py_CLEAR(py_constants_dict);
493+
Py_CLEAR(py_enum_module);
496494

497495
if (py_enum_class == NULL) {
498496
debug_return;
499497
}
500498

499+
// PyModule_AddObject steals the reference to py_enum_class on success
501500
if (PyModule_AddObject(py_module, enum_name, py_enum_class) < 0) {
502501
Py_CLEAR(py_enum_class);
503-
debug_return;
504502
}
505503

506504
debug_return;

0 commit comments

Comments
 (0)