pyldb: Improve search for error string in PyErr_SetLdbError
authorAndrew Bartlett <abartlet@samba.org>
Sun, 24 Mar 2024 23:44:29 +0000 (12:44 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 28 Mar 2024 01:50:41 +0000 (01:50 +0000)
We allow a fallback to ldb_strerror() even if there was an LDB context,
allowing failing functions to reset a previous error string but not
set a new one.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
lib/ldb/pyldb_util.c

index e09114f4ab841e30695da9aa8ad8271fb77fb89c..87407f0eaa6511ca871a545ce8e5fa57dbe4c8df 100644 (file)
@@ -188,11 +188,21 @@ PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn)
 void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
 {
        PyObject *exc = NULL;
+       const char *ldb_error_string = NULL;
+
        if (ret == LDB_ERR_PYTHON_EXCEPTION) {
                return; /* Python exception should already be set, just keep that */
        }
-       exc = Py_BuildValue("(i,s)", ret,
-                           ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx));
+
+       if (ldb_ctx != NULL) {
+               ldb_error_string = ldb_errstring(ldb_ctx);
+       }
+       /* either no LDB context, no string stored or string reset */
+       if (ldb_error_string == NULL) {
+               ldb_error_string = ldb_strerror(ret);
+       }
+
+       exc = Py_BuildValue("(i,s)", ret, ldb_error_string);
        if (exc == NULL) {
                /*
                 * Py_BuildValue failed, and will have set its own exception.