From c3db036163bbb780047dd69725eae01e94825cf7 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 9 Jul 2010 06:53:27 +0300 Subject: [PATCH] s4-ldb: Refactor ldb_msg_diff() to be based on ldb_msg_diff_ex() implementatoin This changes ldb_msg_diff() behavior in two directions: 1. resulting message is allocated in NULL TALLOC_CTX This way it should appear as memory leak in talloc reports in case the caller haven't freed the memory 2. previous implementation had a hidden leak - internal ldb_msg returned from ldb_msg_canonicalize() wasn't freed and stays attached to ldb_context for the connection lifetime --- source4/lib/ldb/common/ldb_msg.c | 50 ++++---------------------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index e54caa8811..186861a243 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -590,55 +590,17 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, struct ldb_message *msg1, struct ldb_message *msg2) { + int ldb_ret; struct ldb_message *mod; - struct ldb_message_element *el; - unsigned int i; - mod = ldb_msg_new(ldb); - if (mod == NULL) { + /* allocate mod message in NULL context + * so it should appear as 'leaked' in talloc reports */ + ldb_ret = ldb_msg_diff_ex(ldb, msg1, msg2, + (TALLOC_CTX*)NULL, &mod); + if (ldb_ret != LDB_SUCCESS) { return NULL; } - mod->dn = msg1->dn; - mod->num_elements = 0; - mod->elements = NULL; - - msg2 = ldb_msg_canonicalize(ldb, msg2); - if (msg2 == NULL) { - talloc_free(mod); - return NULL; - } - - /* look in msg2 to find elements that need to be added - or modified */ - for (i=0;inum_elements;i++) { - el = ldb_msg_find_element(msg1, msg2->elements[i].name); - - if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) { - continue; - } - - if (ldb_msg_add(mod, - &msg2->elements[i], - el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != LDB_SUCCESS) { - talloc_free(mod); - return NULL; - } - } - - /* look in msg1 to find elements that need to be deleted */ - for (i=0;inum_elements;i++) { - el = ldb_msg_find_element(msg2, msg1->elements[i].name); - if (el == NULL) { - if (ldb_msg_add_empty(mod, - msg1->elements[i].name, - LDB_FLAG_MOD_DELETE, NULL) != LDB_SUCCESS) { - talloc_free(mod); - return NULL; - } - } - } - return mod; } -- 2.34.1