From a68428a9510a7d536e6e488323211e972bdd214d Mon Sep 17 00:00:00 2001 From: Joseph Sutton Date: Fri, 30 Sep 2022 11:46:40 +1300 Subject: [PATCH] pyldb: Have functions operating on DNs raise LdbError The return codes of these functions are not often checked. Throwing an exception ensures we won't continue blindly on if DN manipulation fails. Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- lib/ldb/pyldb.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index 3f4b0c7a45c..238a7550deb 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -639,6 +639,7 @@ static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args) { PyObject *py_other; struct ldb_dn *dn, *other; + bool ok; if (!PyArg_ParseTuple(args, "O", &py_other)) return NULL; @@ -647,13 +648,20 @@ static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args) if (!pyldb_Object_AsDn(NULL, py_other, ldb_dn_get_ldb_context(dn), &other)) return NULL; - return PyBool_FromLong(ldb_dn_add_child(dn, other)); + ok = ldb_dn_add_child(dn, other); + if (!ok) { + PyErr_SetLdbError(PyExc_LdbError, LDB_ERR_OPERATIONS_ERROR, NULL); + return NULL; + } + + Py_RETURN_TRUE; } static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args) { PyObject *py_other; struct ldb_dn *other, *dn; + bool ok; if (!PyArg_ParseTuple(args, "O", &py_other)) return NULL; @@ -662,19 +670,32 @@ static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args) if (!pyldb_Object_AsDn(NULL, py_other, ldb_dn_get_ldb_context(dn), &other)) return NULL; - return PyBool_FromLong(ldb_dn_add_base(dn, other)); + ok = ldb_dn_add_base(dn, other); + if (!ok) { + PyErr_SetLdbError(PyExc_LdbError, LDB_ERR_OPERATIONS_ERROR, NULL); + return NULL; + } + + Py_RETURN_TRUE; } static PyObject *py_ldb_dn_remove_base_components(PyLdbDnObject *self, PyObject *args) { struct ldb_dn *dn; int i; + bool ok; if (!PyArg_ParseTuple(args, "i", &i)) return NULL; dn = pyldb_Dn_AS_DN((PyObject *)self); - return PyBool_FromLong(ldb_dn_remove_base_components(dn, i)); + ok = ldb_dn_remove_base_components(dn, i); + if (!ok) { + PyErr_SetLdbError(PyExc_LdbError, LDB_ERR_OPERATIONS_ERROR, NULL); + return NULL; + } + + Py_RETURN_TRUE; } static PyObject *py_ldb_dn_is_child_of(PyLdbDnObject *self, PyObject *args) @@ -819,10 +840,10 @@ static PyMethodDef py_ldb_dn_methods[] = { "S.parent() -> dn\n" "Get the parent for this DN." }, { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS, - "S.add_child(dn) -> None\n" + "S.add_child(dn) -> bool\n" "Add a child DN to this DN." }, { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS, - "S.add_base(dn) -> None\n" + "S.add_base(dn) -> bool\n" "Add a base DN to this DN." }, { "remove_base_components", (PyCFunction)py_ldb_dn_remove_base_components, METH_VARARGS, "S.remove_base_components(int) -> bool\n"