s4:dsdb_sort_objectClass_attr - simplify memory context handling
authorMatthias Dieter Wallnöfer <mdw@samba.org>
Fri, 4 May 2012 06:51:41 +0000 (08:51 +0200)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 21 Aug 2012 23:31:55 +0000 (01:31 +0200)
Do only require the out memory context and build the temporary one in
the body of the function. This greatly simplifies the callers.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
source4/dsdb/pydsdb.c
source4/dsdb/samdb/ldb_modules/objectclass.c
source4/dsdb/schema/schema_query.c

index 39229f487f574c10aa1a0a35d76fa13777edca24..99e239e60c80560a2f673f0a29fe6309fb8a453e 100644 (file)
@@ -681,8 +681,7 @@ static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
        /* Normalise "objectClass" attribute if needed */
        if (ldb_attr_cmp(a->lDAPDisplayName, "objectClass") == 0) {
                int iret;
-               iret = dsdb_sort_objectClass_attr(ldb, schema, tmp_ctx, el,
-                                                tmp_ctx, el);
+               iret = dsdb_sort_objectClass_attr(ldb, schema, el, tmp_ctx, el);
                if (iret != LDB_SUCCESS) {
                        PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb));
                        talloc_free(tmp_ctx);
index 7d34b4e8c3b5df12111fa44f4a0e50f850eb2f75..074360086f9295dcf34196f18f3e59392114c2d5 100644 (file)
@@ -383,7 +383,6 @@ static int objectclass_do_add(struct oc_context *ac)
        struct ldb_request *add_req;
        struct ldb_message_element *objectclass_element, *el;
        struct ldb_message *msg;
-       TALLOC_CTX *mem_ctx;
        const char *rdn_name = NULL;
        char *value;
        const struct dsdb_class *objectclass;
@@ -448,22 +447,14 @@ static int objectclass_do_add(struct oc_context *ac)
                        return LDB_ERR_CONSTRAINT_VIOLATION;
                }
 
-               mem_ctx = talloc_new(ac);
-               if (mem_ctx == NULL) {
-                       return ldb_module_oom(ac->module);
-               }
-
                /* Now do the sorting */
-               ret = dsdb_sort_objectClass_attr(ldb, ac->schema, mem_ctx,
+               ret = dsdb_sort_objectClass_attr(ldb, ac->schema,
                                                 objectclass_element, msg,
                                                 objectclass_element);
                if (ret != LDB_SUCCESS) {
-                       talloc_free(mem_ctx);
                        return ret;
                }
 
-               talloc_free(mem_ctx);
-
                /*
                 * Get the new top-most structural object class and check for
                 * unrelated structural classes
@@ -823,7 +814,6 @@ static int objectclass_do_mod(struct oc_context *ac)
        struct ldb_message_element *oc_el_entry, *oc_el_change;
        struct ldb_val *vals;
        struct ldb_message *msg;
-       TALLOC_CTX *mem_ctx;
        const struct dsdb_class *objectclass;
        unsigned int i, j, k;
        bool found;
@@ -851,11 +841,6 @@ static int objectclass_do_mod(struct oc_context *ac)
 
        msg->dn = ac->req->op.mod.message->dn;
 
-       mem_ctx = talloc_new(ac);
-       if (mem_ctx == NULL) {
-               return ldb_module_oom(ac->module);
-       }
-
        /* We've to walk over all "objectClass" message elements */
        for (k = 0; k < ac->req->op.mod.message->num_elements; k++) {
                if (ldb_attr_cmp(ac->req->op.mod.message->elements[k].name,
@@ -876,7 +861,6 @@ static int objectclass_do_mod(struct oc_context *ac)
                                                                       "objectclass: cannot re-add an existing objectclass: '%.*s'!",
                                                                       (int)oc_el_change->values[i].length,
                                                                       (const char *)oc_el_change->values[i].data);
-                                               talloc_free(mem_ctx);
                                                return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
                                        }
                                }
@@ -886,7 +870,6 @@ static int objectclass_do_mod(struct oc_context *ac)
                                                      struct ldb_val,
                                                      oc_el_entry->num_values + 1);
                                if (vals == NULL) {
-                                       talloc_free(mem_ctx);
                                        return ldb_module_oom(ac->module);
                                }
                                oc_el_entry->values = vals;
@@ -933,7 +916,6 @@ static int objectclass_do_mod(struct oc_context *ac)
                                                               "objectclass: cannot delete this objectclass: '%.*s'!",
                                                               (int)oc_el_change->values[i].length,
                                                               (const char *)oc_el_change->values[i].data);
-                                       talloc_free(mem_ctx);
                                        return LDB_ERR_NO_SUCH_ATTRIBUTE;
                                }
                        }
@@ -942,10 +924,9 @@ static int objectclass_do_mod(struct oc_context *ac)
                }
 
                /* Now do the sorting */
-               ret = dsdb_sort_objectClass_attr(ldb, ac->schema, mem_ctx,
-                                                oc_el_entry, msg, oc_el_entry);
+               ret = dsdb_sort_objectClass_attr(ldb, ac->schema, oc_el_entry,
+                                                msg, oc_el_entry);
                if (ret != LDB_SUCCESS) {
-                       talloc_free(mem_ctx);
                        return ret;
                }
 
@@ -958,7 +939,6 @@ static int objectclass_do_mod(struct oc_context *ac)
                if (objectclass == NULL) {
                        ldb_set_errstring(ldb,
                                          "objectclass: cannot delete all structural objectclasses!");
-                       talloc_free(mem_ctx);
                        return LDB_ERR_OBJECT_CLASS_VIOLATION;
                }
 
@@ -967,13 +947,10 @@ static int objectclass_do_mod(struct oc_context *ac)
                                                    objectclass,
                                                    oc_el_entry);
                if (ret != LDB_SUCCESS) {
-                       talloc_free(mem_ctx);
                        return ret;
                }
        }
 
-       talloc_free(mem_ctx);
-
        /* Now add the new object class attribute to the change message */
        ret = ldb_msg_add(msg, oc_el_entry, LDB_FLAG_MOD_REPLACE);
        if (ret != LDB_SUCCESS) {
index ce46fa6315692a5d9310c9b7896c4d32c66f9854..013878d1f1970167b4031987b44efa5a0e111e63 100644 (file)
@@ -451,14 +451,12 @@ const struct GUID *attribute_schemaid_guid_by_lDAPDisplayName(const struct dsdb_
  * into correct order and validate that all object classes specified actually
  * exist in the schema.
  * The output is written in an existing LDB message element
- * "out_objectclass_element" where the values will be allocated on
- * "out_mem_ctx".
+ * "out_objectclass_element" where the values will be allocated on "mem_ctx".
  */
 int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
                               const struct dsdb_schema *schema,
-                              TALLOC_CTX *mem_ctx,
                               const struct ldb_message_element *objectclass_element,
-                              TALLOC_CTX *out_mem_ctx,
+                              TALLOC_CTX *mem_ctx,
                               struct ldb_message_element *out_objectclass_element)
 {
        unsigned int i, lowest;
@@ -469,6 +467,12 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
          *poss_parent = NULL, *new_parent = NULL,
          *current_lowest = NULL, *current_lowest_struct = NULL;
        struct ldb_message_element *el;
+       TALLOC_CTX *tmp_mem_ctx;
+
+       tmp_mem_ctx = talloc_new(mem_ctx);
+       if (tmp_mem_ctx == NULL) {
+               return ldb_oom(ldb);
+       }
 
        /*
         * DESIGN:
@@ -504,8 +508,9 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
         * except for 'top', which is special
         */
        for (i=0; i < objectclass_element->num_values; i++) {
-               current = talloc(mem_ctx, struct class_list);
+               current = talloc(tmp_mem_ctx, struct class_list);
                if (!current) {
+                       talloc_free(tmp_mem_ctx);
                        return ldb_oom(ldb);
                }
                current->objectclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &objectclass_element->values[i]);
@@ -513,11 +518,13 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
                        ldb_asprintf_errstring(ldb, "objectclass %.*s is not a valid objectClass in schema",
                                               (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
                        /* This looks weird, but windows apparently returns this for invalid objectClass values */
+                       talloc_free(tmp_mem_ctx);
                        return LDB_ERR_NO_SUCH_ATTRIBUTE;
                } else if (current->objectclass->isDefunct) {
                        ldb_asprintf_errstring(ldb, "objectclass %.*s marked as isDefunct objectClass in schema - not valid for new objects",
                                               (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
                        /* This looks weird, but windows apparently returns this for invalid objectClass values */
+                       talloc_free(tmp_mem_ctx);
                        return LDB_ERR_NO_SUCH_ATTRIBUTE;
                }
 
@@ -529,7 +536,7 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
 
 
        /* Add top here, to prevent duplicates */
-       current = talloc(mem_ctx, struct class_list);
+       current = talloc(tmp_mem_ctx, struct class_list);
        current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top");
        DLIST_ADD_END(sorted, current, struct class_list *);
 
@@ -545,7 +552,7 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
                        continue;
                }
 
-               new_parent = talloc(mem_ctx, struct class_list);
+               new_parent = talloc(tmp_mem_ctx, struct class_list);
                new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf);
                DLIST_ADD_END(unsorted, new_parent, struct class_list *);
        }
@@ -583,16 +590,18 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
        el = out_objectclass_element;
 
        el->flags = objectclass_element->flags;
-       el->name = talloc_strdup(out_mem_ctx, objectclass_element->name);
+       el->name = talloc_strdup(mem_ctx, objectclass_element->name);
        if (el->name == NULL) {
+               talloc_free(tmp_mem_ctx);
                return ldb_oom(ldb);
        }
        el->num_values = 0;
        el->values = NULL;
        for (current = sorted; current != NULL; current = current->next) {
-               el->values = talloc_realloc(out_mem_ctx, el->values,
+               el->values = talloc_realloc(mem_ctx, el->values,
                                            struct ldb_val, el->num_values + 1);
                if (el->values == NULL) {
+                       talloc_free(tmp_mem_ctx);
                        return ldb_oom(ldb);
                }
                el->values[el->num_values] = data_blob_string_const(current->objectclass->lDAPDisplayName);
@@ -600,5 +609,6 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
                ++(el->num_values);
        }
 
+       talloc_free(tmp_mem_ctx);
        return LDB_SUCCESS;
 }