dsdb: Replace talloc_steal() with a shallow copy and reference in dsdb_paged_results
authorAndrew Bartlett <abartlet@samba.org>
Wed, 2 Aug 2023 02:12:07 +0000 (14:12 +1200)
committerStefan Metzmacher <metze@samba.org>
Wed, 2 Aug 2023 11:16:41 +0000 (11:16 +0000)
We should not be stealing caller memory like this, and while a
talloc_reference() is not much better, this combined with a
shallow copy should be a little better in terms of polite
memory management.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15442

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
source4/dsdb/samdb/ldb_modules/paged_results.c

index a6f2bf22098fdad266f1466570509eccaf803d55..83729b02c0b9cb42062d755112e2871abec72e1d 100644 (file)
@@ -681,6 +681,7 @@ static int paged_search(struct ldb_module *module, struct ldb_request *req)
                struct ldb_control *ext_ctrl;
                struct ldb_control **controls;
                static const char * const attrs[1] = { NULL };
+               void *ref = NULL;
 
                if (paged_ctrl->size == 0) {
                        return LDB_ERR_OPERATIONS_ERROR;
@@ -739,7 +740,25 @@ static int paged_search(struct ldb_module *module, struct ldb_request *req)
                        return ret;
                }
 
-               ac->store->expr = talloc_steal(ac->store, req->op.search.tree);
+               /*
+                * LDB does not have a function to take a full copy of
+                * this, but at least take a shallow copy
+                */
+               ac->store->expr = ldb_parse_tree_copy_shallow(ac->store,
+                                                             req->op.search.tree);
+
+               if (ac->store->expr == NULL) {
+                       return ldb_operr(ldb);
+               }
+
+               /*
+                * As the above is only a shallow copy, take a
+                * reference to ensure the values are kept around
+                */
+               ref = talloc_reference(ac->store, req->op.search.tree);
+               if (ref == NULL) {
+                       return ldb_module_oom(module);
+               }
                ac->store->expr_str = ldb_filter_from_tree(ac->store,
                                                          req->op.search.tree);
                if (ac->store->expr_str == NULL) {