ldb: don't try to save a value that isn't there
[samba.git] / lib / ldb / common / ldb_dn.c
index 9b2fa966e111711249a0815e5e3a914861bc29db..a7fb0d9c44354323fb549c63f4b17b08afaae85b 100644 (file)
@@ -697,31 +697,32 @@ static bool ldb_dn_explode(struct ldb_dn *dn)
                goto failed;
        }
 
-       /* save last element */
-       if ( t ) {
-               /* trim back */
-               d -= (p - t);
-               l -= (p - t);
-       }
+       if (in_value) {
+               /* save last element */
+               if ( t ) {
+                       /* trim back */
+                       d -= (p - t);
+                       l -= (p - t);
+               }
+
+               *d++ = '\0';
+               /*
+                * This talloc_memdup() is OK with the
+                * +1 because *d has been set to '\0'
+                * just above.
+                */
+               dn->components[dn->comp_num].value.length = l;
+               dn->components[dn->comp_num].value.data =
+                       (uint8_t *)talloc_memdup(dn->components, dt, l + 1);
+               if ( ! dn->components[dn->comp_num].value.data) {
+                       /* ouch */
+                       goto failed;
+               }
+               talloc_set_name_const(dn->components[dn->comp_num].value.data,
+                       (const char *)dn->components[dn->comp_num].value.data);
 
-       *d++ = '\0';
-       /*
-        * This talloc_memdup() is OK with the
-        * +1 because *d has been set to '\0'
-        * just above.
-        */
-       dn->components[dn->comp_num].value.length = l;
-       dn->components[dn->comp_num].value.data =
-               (uint8_t *)talloc_memdup(dn->components, dt, l + 1);
-       if ( ! dn->components[dn->comp_num].value.data) {
-               /* ouch */
-               goto failed;
+               dn->comp_num++;
        }
-       talloc_set_name_const(dn->components[dn->comp_num].value.data,
-                             (const char *)dn->components[dn->comp_num].value.data);
-
-       dn->comp_num++;
-
        talloc_free(data);
        return true;