ldb:mod:sort: rearrange NULL checks
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Sun, 7 Apr 2024 02:54:34 +0000 (14:54 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 23 Apr 2024 01:33:29 +0000 (01:33 +0000)
There are further changes coming here.

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>
lib/ldb/modules/sort.c

index cb6f8df440f2ec07557277d182129cfab47f1ec1..8487c7003b6a675a109d18cd6698f8f188c33bef 100644 (file)
@@ -121,15 +121,18 @@ static int sort_compare(struct ldb_message **msg1, struct ldb_message **msg2, vo
        el1 = ldb_msg_find_element(*msg1, ac->attributeName);
        el2 = ldb_msg_find_element(*msg2, ac->attributeName);
 
-       if (!el1 && el2) {
+       /*
+        * NULL elements sort at the end (regardless of ac->reverse flag).
+        */
+       if (el1 == NULL && el2 == NULL) {
+               return 0;
+       }
+       if (el1 == NULL) {
                return 1;
        }
-       if (el1 && !el2) {
+       if (el2 == NULL) {
                return -1;
        }
-       if (!el1 && !el2) {
-               return 0;
-       }
 
        if (ac->reverse)
                return ac->a->syntax->comparison_fn(ldb, ac, &el2->values[0], &el1->values[0]);