s4:rpcsrv:dnsserver: make dns_name_compare transitive with NULLs
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Mon, 8 Apr 2024 10:54:49 +0000 (22:54 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 23 Apr 2024 01:33:29 +0000 (01:33 +0000)
Returning 0 on `(name1 == NULL || name2 == NULL)` made NULL equal to
everything, which confuses a sort (consider {A, B, NULL} where A > B,
but A == NULL == B).

The only caller is dnsserver_enumerate_records() which fails if it
finds a NULL in the sorted list. We make the happen more quickly by
sorting NULLs to the front.

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>
source4/rpc_server/dnsserver/dnsdata.c

index e6d35fc00fc55c938c85e83abd271d79c7236b7a..6ffca196861d881ec03c2be505dde26758064173 100644 (file)
@@ -1075,9 +1075,23 @@ int dns_name_compare(struct ldb_message * const *m1, struct ldb_message * const
 
        name1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
        name2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
-       if (name1 == NULL || name2 == NULL) {
+       /*
+        * We sort NULL names to the start of the list, because the only
+        * caller of this function, dnsserver_enumerate_records() will call
+        * dns_build_tree() with the sorted list, which will always return an
+        * error when it hits a NULL, so we might as well make that happen
+        * quickly.
+        */
+       if (name1 == name2) {
+               /* this includes the both NULL case */
                return 0;
        }
+       if (name1 == NULL) {
+               return -1;
+       }
+       if (name2 == NULL) {
+               return 1;
+       }
 
        /* Compare the last components of names.
         * If search_name is not NULL, compare the second last components of names */