s4-ldb: Refactor ldb_msg_canonicalize() to be based on ldb_msg_canonicalize_ex()...
authorKamen Mazdrashki <kamenim@samba.org>
Sat, 10 Jul 2010 00:19:29 +0000 (03:19 +0300)
committerKamen Mazdrashki <kamenim@samba.org>
Sat, 10 Jul 2010 20:03:16 +0000 (23:03 +0300)
This changes slightly previous behavior so that
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

source4/lib/ldb/common/ldb_msg.c

index 806f0e8..4b32711 100644 (file)
@@ -576,41 +576,20 @@ failed:
 }
 
 
-/*
 canonicalise a message, merging elements of the same name
-*/
+/**
* Canonicalize a message, merging elements of the same name
+ */
 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
                                         const struct ldb_message *msg)
 {
-       unsigned int i;
+       int ret;
        struct ldb_message *msg2;
 
-       msg2 = ldb_msg_copy(ldb, msg);
-       if (msg2 == NULL) return NULL;
-
-       ldb_msg_sort_elements(msg2);
-
-       for (i=1;i<msg2->num_elements;i++) {
-               struct ldb_message_element *el1 = &msg2->elements[i-1];
-               struct ldb_message_element *el2 = &msg2->elements[i];
-               if (ldb_msg_element_compare_name(el1, el2) == 0) {
-                       el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val, 
-                                                      el1->num_values + el2->num_values);
-                       if (el1->num_values + el2->num_values > 0 && el1->values == NULL) {
-                               return NULL;
-                       }
-                       memcpy(el1->values + el1->num_values,
-                              el2->values,
-                              sizeof(struct ldb_val) * el2->num_values);
-                       el1->num_values += el2->num_values;
-                       talloc_free(discard_const_p(char, el2->name));
-                       if (i+1<msg2->num_elements) {
-                               memmove(el2, el2+1, sizeof(struct ldb_message_element) * 
-                                       (msg2->num_elements - (i+1)));
-                       }
-                       msg2->num_elements--;
-                       i--;
-               }
+       /* allocate msg2 message in NULL context
+        * so it should appear as 'leaked' in talloc reports */
+       ret = ldb_msg_canonicalize_ex(ldb, msg, (TALLOC_CTX*)NULL, &msg2);
+       if (ret != LDB_SUCCESS) {
+               return NULL;
        }
 
        return msg2;