From: Douglas Bagnall Date: Thu, 14 Mar 2024 02:10:07 +0000 (+1300) Subject: ldb:pyldb: PyErr_LDB_DN_OR_RAISE makes more rigourous checks X-Git-Tag: tdb-1.4.11~1218 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f98035a2a3147f7d935a356d2a273e0bfd0796f2;p=thirdparty%2Fsamba.git ldb:pyldb: PyErr_LDB_DN_OR_RAISE makes more rigourous checks This changes what happens all over the place (lib/ldb/pyldb.c, source4/dns_server/pydns.c, source4/dsdb/pydsdb.c), but causes no problems because it just checks what we always assumed. Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/lib/ldb/pyldb.h b/lib/ldb/pyldb.h index 1c6372c2ed9..5487def58a0 100644 --- a/lib/ldb/pyldb.h +++ b/lib/ldb/pyldb.h @@ -66,12 +66,31 @@ PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn, PyLdbObject *pyldb); bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, struct ldb_context *ldb_ctx, struct ldb_dn **dn); #define pyldb_Dn_AS_DN(pyobj) ((PyLdbDnObject *)pyobj)->dn -#define PyErr_LDB_DN_OR_RAISE(py_ldb_dn, dn) \ - if (!pyldb_check_type(py_ldb_dn, "Dn")) { \ + +/* + * PyErr_LDB_DN_OR_RAISE does 3 things: + * 1. checks that a PyObject is really a PyLdbDnObject. + * 2. checks that the ldb that the PyLdbDnObject knows is the ldb that its dn + * knows. + * 3. sets the (struct ldb_dn *) dn argument to the dn the pyobject refers to. + * + * why so much? because we almost always need it. + */ +#define PyErr_LDB_DN_OR_RAISE(_py_obj, dn) do { \ + PyLdbDnObject *_py_dn = NULL; \ + if (!pyldb_check_type(_py_obj, "Dn")) { \ PyErr_SetString(PyExc_TypeError, "ldb Dn object required"); \ - return NULL; \ - } \ - dn = pyldb_Dn_AS_DN(py_ldb_dn); + return NULL; \ + } \ + _py_dn = (PyLdbDnObject *)_py_obj; \ + dn = pyldb_Dn_AS_DN(_py_dn); \ + if (_py_dn->pyldb->ldb_ctx != ldb_dn_get_ldb_context(dn)) { \ + PyErr_SetString(PyExc_RuntimeError, \ + "Dn has a stale LDB connection"); \ + return NULL; \ + } \ +} while(0) + bool pyldb_check_type(PyObject *obj, const char *type_name);