Skip to content

Commit

Permalink
sudo_module_register_loghandler: clear sudo_type_LogHandler on error
Browse files Browse the repository at this point in the history
Also add comments about PyModule_AddObject stealing a ref on success.
  • Loading branch information
millert committed Jul 22, 2023
1 parent b52c32f commit 0462a4e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions plugins/python/python_baseplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ sudo_module_register_baseplugin(PyObject *py_module)
goto cleanup;
}

// PyModule_AddObject steals a reference to py_class on success
Py_INCREF(py_class);
rc = SUDO_RC_OK;

Expand Down
1 change: 1 addition & 0 deletions plugins/python/python_convmessage.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ sudo_module_register_conv_message(PyObject *py_module)
goto cleanup;
}

// PyModule_AddObject steals the reference to py_class on success
Py_INCREF(py_class);
rc = SUDO_RC_OK;

Expand Down
5 changes: 4 additions & 1 deletion plugins/python/python_loghandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ sudo_module_register_loghandler(PyObject *py_module)
if (sudo_type_LogHandler == NULL)
goto cleanup;

if (PyModule_AddObject(py_module, "LogHandler", sudo_type_LogHandler) < 0)
if (PyModule_AddObject(py_module, "LogHandler", sudo_type_LogHandler) < 0) {
Py_CLEAR(sudo_type_LogHandler);
goto cleanup;
}

// PyModule_AddObject steals a reference to sudo_type_LogHandler on success
Py_INCREF(sudo_type_LogHandler);

cleanup:
Expand Down
24 changes: 11 additions & 13 deletions plugins/python/sudo_python_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,28 +479,26 @@ sudo_module_register_enum(PyObject *py_module, const char *enum_name, PyObject *
return;

PyObject *py_enum_class = NULL;
{
PyObject *py_enum_module = PyImport_ImportModule("enum");
if (py_enum_module == NULL) {
Py_CLEAR(py_constants_dict);
debug_return;
}
PyObject *py_enum_module = PyImport_ImportModule("enum");
if (py_enum_module == NULL) {
Py_CLEAR(py_constants_dict);
debug_return;
}

py_enum_class = PyObject_CallMethod(py_enum_module,
"IntEnum", "sO", enum_name,
py_constants_dict);
py_enum_class = PyObject_CallMethod(py_enum_module,
"IntEnum", "sO", enum_name,
py_constants_dict);

Py_CLEAR(py_constants_dict);
Py_CLEAR(py_enum_module);
}
Py_CLEAR(py_constants_dict);
Py_CLEAR(py_enum_module);

if (py_enum_class == NULL) {
debug_return;
}

// PyModule_AddObject steals the reference to py_enum_class on success
if (PyModule_AddObject(py_module, enum_name, py_enum_class) < 0) {
Py_CLEAR(py_enum_class);
debug_return;
}

debug_return;
Expand Down

0 comments on commit 0462a4e

Please sign in to comment.