From 1d8024e733e9717e86883c03092264fbcf25ac1d Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Wed, 25 Oct 2023 13:15:36 +1300 Subject: [PATCH] lib/ldb: py LDBError avoids leak and checks for alloc failure Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- lib/ldb/pyldb.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index 49641957223..d750a0b97dd 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -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;