]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib/ldb: py LDBError avoids leak and checks for alloc failure
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 25 Oct 2023 00:15:36 +0000 (13:15 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 1 Nov 2023 20:10:46 +0000 (20:10 +0000)
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/ldb/pyldb.c

index 49641957223a4687083a5eadb32484e99a2e28b2..d750a0b97dd813cd6ea7af6793234e1062dd784d 100644 (file)
@@ -266,13 +266,25 @@ static PyTypeObject PyLdbControl = {
 
 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
 {
-       if (ret == LDB_ERR_PYTHON_EXCEPTION)
+       PyObject *exc = NULL;
+       if (ret == LDB_ERR_PYTHON_EXCEPTION) {
                return; /* Python exception should already be set, just keep that */
-
-       PyErr_SetObject(error, 
-                       Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
-                                     ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
+       }
+       exc = Py_BuildValue("(i,s)", ret,
+                           ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx));
+       if (exc == NULL) {
+               /*
+                * Py_BuildValue failed, and will have set its own exception.
+                * It isn't the one we wanted, but it will have to do.
+                * This is all very unexpected.
+                */
+               fprintf(stderr, "could not make LdbError %d!\n", ret);
+               return;
+       }
+       PyErr_SetObject(error, exc);
+       Py_DECREF(exc);
 }
+
 static PyObject *py_ldb_bytes_str(PyBytesObject *self)
 {
        char *msg = NULL;