]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ldb:dn: make ldb_dn_compare() self-consistent
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Sun, 7 Apr 2024 03:04:43 +0000 (15:04 +1200)
committerJule Anger <janger@samba.org>
Mon, 10 Jun 2024 13:25:17 +0000 (13:25 +0000)
We were returning -1 in all these cases:

   ldb_dn_compare(dn, NULL);
   ldb_dn_compare(NULL, dn);
   ldb_dn_compare(NULL, NULL);

which would give strange results in sort, where this is often used.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15625

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
(cherry picked from commit 5fe488d515a8bb719bdeafb8b64d8479732b5ac8)

lib/ldb/common/ldb_dn.c

index 7c0f0a2197bf5e3c0e385f972c2fe822af455c17..92fa223ceb7859216c03287e5ce318300a9fbc12 100644 (file)
@@ -1132,8 +1132,22 @@ int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
 {
        unsigned int i;
        int ret;
+       /*
+        * If used in sort, we shift NULL and invalid DNs to the end.
+        *
+        * If ldb_dn_casefold_internal() fails, that goes to the end too, so
+        * we end up with:
+        *
+        * | normal DNs, sorted | casefold failed DNs | invalid DNs | NULLs |
+        */
 
-       if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) {
+       if (dn0 == dn1 || (dn0->invalid && dn1->invalid)) {
+               return 0;
+       }
+       if (dn0 == NULL || dn0->invalid) {
+               return 1;
+       }
+       if (dn1 == NULL || dn1->invalid) {
                return -1;
        }