ldb_tdb: replace strange dn_list_cmp() in index code
authorAndrew Bartlett <abartlet@samba.org>
Thu, 10 Aug 2017 05:00:48 +0000 (17:00 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 22 Sep 2017 19:20:20 +0000 (21:20 +0200)
This replaces dn_list_cmp() with functions that do not attempt to
to care about string termination.  All index values are case sensitive
and correctly length-bound already, even for a DN index
so just use a length check and memcmp()

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
lib/ldb/ldb_tdb/ldb_index.c

index 6a61c2ea2255efc1be5e33881c57be7290b6bebb..1bf6f3cccdd81ccf7cd3f737845c8358e76af767 100644 (file)
@@ -61,23 +61,28 @@ int ltdb_index_transaction_start(struct ldb_module *module)
        return LDB_SUCCESS;
 }
 
-/* compare two DN entries in a dn_list. Take account of possible
- * differences in string termination */
-static int dn_list_cmp(const struct ldb_val *v1, const struct ldb_val *v2)
+/*
+  see if two ldb_val structures contain exactly the same data
+  return -1 or 1 for a mismatch, 0 for match
+*/
+static int ldb_val_equal_exact_for_qsort(const struct ldb_val *v1,
+                                        const struct ldb_val *v2)
 {
-       if (v1->length > v2->length && v1->data[v2->length] != 0) {
+       if (v1->length > v2->length) {
                return -1;
        }
-       if (v1->length < v2->length && v2->data[v1->length] != 0) {
+       if (v1->length < v2->length) {
                return 1;
        }
-       return strncmp((char *)v1->data, (char *)v2->data, v1->length);
+       return memcmp(v1->data, v2->data, v1->length);
 }
 
 
 /*
   find a entry in a dn_list, using a ldb_val. Uses a case sensitive
-  comparison with the dn returns -1 if not found
+  binary-safe comparison for the 'dn' returns -1 if not found
+
+  This is therefore safe when the value is a GUID in the future
  */
 static int ltdb_dn_list_find_val(struct ltdb_private *ltdb,
                                 const struct dn_list *list,
@@ -85,7 +90,7 @@ static int ltdb_dn_list_find_val(struct ltdb_private *ltdb,
 {
        unsigned int i;
        for (i=0; i<list->count; i++) {
-               if (dn_list_cmp(&list->dn[i], v) == 0) {
+               if (ldb_val_equal_exact(&list->dn[i], v) == 1) {
                        return i;
                }
        }
@@ -1058,11 +1063,13 @@ static void ltdb_dn_list_remove_duplicates(struct dn_list *list)
                return;
        }
 
-       TYPESAFE_QSORT(list->dn, list->count, dn_list_cmp);
+       TYPESAFE_QSORT(list->dn, list->count,
+                      ldb_val_equal_exact_for_qsort);
 
        new_count = 1;
        for (i=1; i<list->count; i++) {
-               if (dn_list_cmp(&list->dn[i], &list->dn[new_count-1]) != 0) {
+               if (ldb_val_equal_exact(&list->dn[i],
+                                       &list->dn[new_count-1]) == 0) {
                        if (new_count != i) {
                                list->dn[new_count] = list->dn[i];
                        }