1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

ldb: reduce non-transitive comparisons in ldb_msg_element_compare()

We can still have inconsistent comparisons, because two elements with
the same number of values will always return -1 if they are unequal,
which means they will sort differently depending on the order in which
they are compared.

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>
This commit is contained in:
Douglas Bagnall 2024-04-04 11:26:25 +13:00 committed by Andrew Bartlett
parent 5c36bc8241
commit 21a071e486

View File

@ -749,9 +749,16 @@ int ldb_msg_element_compare(struct ldb_message_element *el1,
unsigned int i; unsigned int i;
if (el1->num_values != el2->num_values) { if (el1->num_values != el2->num_values) {
return el1->num_values - el2->num_values; return NUMERIC_CMP(el1->num_values, el2->num_values);
} }
/*
* Note this is an inconsistent comparison, unsuitable for
* sorting. If A has values {a, b} and B has values {b, c},
* then
*
* ldb_msg_element_compare(A, B) returns -1, meaning A < B
* ldb_msg_element_compare(B, A) returns -1, meaning B < A
*/
for (i=0;i<el1->num_values;i++) { for (i=0;i<el1->num_values;i++) {
if (!ldb_msg_find_val(el2, &el1->values[i])) { if (!ldb_msg_find_val(el2, &el1->values[i])) {
return -1; return -1;