pyldb: make py_ldb_dn_add_base() a bit less leaky
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Thu, 14 Mar 2024 23:38:00 +0000 (12:38 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 10 Apr 2024 05:13:32 +0000 (05:13 +0000)
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/ldb/pyldb.c

index 3786ca284a7c71a98fd5ef6bc2df8ada87e1e587..18613f59f2f793358cc18b2ed259f976f43a5720 100644 (file)
@@ -697,25 +697,43 @@ static PyObject *py_ldb_dn_add_child(PyObject *self, PyObject *args)
 
 static PyObject *py_ldb_dn_add_base(PyObject *self, PyObject *args)
 {
-       PyObject *py_other;
+       PyObject *py_other = NULL;
        struct ldb_dn *other = NULL;
        struct ldb_dn *dn = NULL;
+       TALLOC_CTX *tmp_ctx = NULL;
        bool ok;
 
        PyErr_LDB_DN_OR_RAISE(self, dn);
 
-       if (!PyArg_ParseTuple(args, "O", &py_other))
+       if (!PyArg_ParseTuple(args, "O", &py_other)) {
                return NULL;
+       }
 
-       if (!pyldb_Object_AsDn(NULL, py_other, ldb_dn_get_ldb_context(dn), &other))
+       /*
+        * As noted in py_ldb_dn_add_child() comments, if py_other is a
+        * string, other is an ephemeral struct ldb_dn, but if py_other is a
+        * python DN, other points to the corresponding long-lived DN.
+        */
+       tmp_ctx = talloc_new(NULL);
+       if (tmp_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       ok = pyldb_Object_AsDn(tmp_ctx,
+                              py_other,
+                              ldb_dn_get_ldb_context(dn),
+                              &other);
+       if (!ok) {
+               TALLOC_FREE(tmp_ctx);
                return NULL;
+       }
 
        ok = ldb_dn_add_base(dn, other);
+       TALLOC_FREE(tmp_ctx);
        if (!ok) {
                PyErr_SetLdbError(PyExc_LdbError, LDB_ERR_OPERATIONS_ERROR, NULL);
                return NULL;
        }
-
        Py_RETURN_TRUE;
 }