ldb:ldb_dn.c - "ldb_dn_set_extended_component" - free the linearized string when...
[kamenim/samba.git] / source4 / lib / ldb / common / ldb_dn.c
index 00646f6ba54418dcac2d57bee3e671a9980eaa5f..11de31ed6b0fcdfc3b9fdad4643401469f511c09 100644 (file)
@@ -331,8 +331,9 @@ static bool ldb_dn_explode(struct ldb_dn *dn)
                return true;
        }
 
-       /* make sure we free this if alloced previously before replacing */
-       talloc_free(dn->components);
+       /* make sure we free this if allocated previously before replacing */
+       LDB_FREE(dn->components);
+       dn->comp_num = 0;
 
        LDB_FREE(dn->ext_components);
        dn->ext_comp_num = 0;
@@ -343,7 +344,6 @@ static bool ldb_dn_explode(struct ldb_dn *dn)
        if ( ! dn->components) {
                return false;
        }
-       dn->comp_num = 0;
 
        /* Components data space is allocated here once */
        data = talloc_array(dn->components, char, strlen(parse_dn) + 1);
@@ -1431,9 +1431,9 @@ bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
        /* Wipe the ext_linearized DN,
         * the GUID and SID are almost certainly no longer valid */
        LDB_FREE(dn->ext_linearized);
-
        LDB_FREE(dn->ext_components);
        dn->ext_comp_num = 0;
+
        return true;
 }
 
@@ -1488,6 +1488,10 @@ bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
                unsigned int n;
                long long int i, j;
 
+               if (dn->comp_num == 0) {
+                       return false;
+               }
+
                if ( ! ldb_dn_validate(child)) {
                        return false;
                }
@@ -1534,6 +1538,9 @@ bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
        }
 
        if (dn->linearized) {
+               if (dn->linearized[0] == '\0') {
+                       return false;
+               }
 
                s = ldb_dn_get_linearized(child);
                if ( ! s) {
@@ -1552,7 +1559,6 @@ bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
        /* Wipe the ext_linearized DN,
         * the GUID and SID are almost certainly no longer valid */
        LDB_FREE(dn->ext_linearized);
-
        LDB_FREE(dn->ext_components);
        dn->ext_comp_num = 0;
 
@@ -1628,7 +1634,6 @@ bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
        /* Wipe the ext_linearized DN,
         * the GUID and SID are almost certainly no longer valid */
        LDB_FREE(dn->ext_linearized);
-
        LDB_FREE(dn->ext_components);
        dn->ext_comp_num = 0;
 
@@ -1673,9 +1678,9 @@ bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
        /* Wipe the ext_linearized DN,
         * the GUID and SID are almost certainly no longer valid */
        LDB_FREE(dn->ext_linearized);
-
        LDB_FREE(dn->ext_components);
        dn->ext_comp_num = 0;
+
        return true;
 }
 
@@ -1696,9 +1701,9 @@ struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, struct ldb_dn *dn)
        /* Wipe the ext_linearized DN,
         * the GUID and SID are almost certainly no longer valid */
        LDB_FREE(dn->ext_linearized);
-
        LDB_FREE(dn->ext_components);
        dn->ext_comp_num = 0;
+
        return new_dn;
 }
 
@@ -1870,9 +1875,9 @@ int ldb_dn_set_component(struct ldb_dn *dn, int num,
        /* Wipe the ext_linearized DN,
         * the GUID and SID are almost certainly no longer valid */
        LDB_FREE(dn->ext_linearized);
-
-       dn->ext_comp_num = 0;
        LDB_FREE(dn->ext_components);
+       dn->ext_comp_num = 0;
+
        return LDB_SUCCESS;
 }
 
@@ -1920,7 +1925,6 @@ int ldb_dn_set_extended_component(struct ldb_dn *dn,
                                        ldb_dn_mark_invalid(dn);
                                        return LDB_ERR_OPERATIONS_ERROR;
                                }
-                               return LDB_SUCCESS;
                        } else {
                                if (i != (dn->ext_comp_num - 1)) {
                                        memmove(&dn->ext_components[i],
@@ -1938,9 +1942,10 @@ int ldb_dn_set_extended_component(struct ldb_dn *dn,
                                        ldb_dn_mark_invalid(dn);
                                        return LDB_ERR_OPERATIONS_ERROR;
                                }
-                               return LDB_SUCCESS;
                        }
                        LDB_FREE(dn->ext_linearized);
+
+                       return LDB_SUCCESS;
                }
        }
 
@@ -1971,14 +1976,16 @@ int ldb_dn_set_extended_component(struct ldb_dn *dn,
        dn->ext_components = p;
        dn->ext_comp_num++;
 
+       LDB_FREE(dn->ext_linearized);
+
        return LDB_SUCCESS;
 }
 
 void ldb_dn_remove_extended_components(struct ldb_dn *dn)
 {
-       dn->ext_comp_num = 0;
-       LDB_FREE(dn->ext_components);
        LDB_FREE(dn->ext_linearized);
+       LDB_FREE(dn->ext_components);
+       dn->ext_comp_num = 0;
 }
 
 bool ldb_dn_is_valid(struct ldb_dn *dn)