diff --git a/src/basic/hash-funcs.c b/src/basic/hash-funcs.c index db48437be7..6f6832d237 100644 --- a/src/basic/hash-funcs.c +++ b/src/basic/hash-funcs.c @@ -71,7 +71,7 @@ void trivial_hash_func(const void *p, struct siphash *state) { } int trivial_compare_func(const void *a, const void *b) { - return a < b ? -1 : (a > b ? 1 : 0); + return CMP(a, b); } const struct hash_ops trivial_hash_ops = { @@ -87,7 +87,7 @@ int uint64_compare_func(const void *_a, const void *_b) { uint64_t a, b; a = *(const uint64_t*) _a; b = *(const uint64_t*) _b; - return a < b ? -1 : (a > b ? 1 : 0); + return CMP(a, b); } const struct hash_ops uint64_hash_ops = { @@ -104,7 +104,7 @@ int devt_compare_func(const void *_a, const void *_b) { dev_t a, b; a = *(const dev_t*) _a; b = *(const dev_t*) _b; - return a < b ? -1 : (a > b ? 1 : 0); + return CMP(a, b); } const struct hash_ops devt_hash_ops = { diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index a21aa149f5..d83658eaa7 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -581,9 +581,11 @@ void in_addr_data_hash_func(const void *p, struct siphash *state) { int in_addr_data_compare_func(const void *a, const void *b) { const struct in_addr_data *x = a, *y = b; + int r; - if (x->family != y->family) - return x->family - y->family; + r = CMP(x->family, y->family); + if (r != 0) + return r; return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family)); } diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 0a5280eb24..d6ee8b62b8 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1108,12 +1108,7 @@ int pid_compare_func(const void *a, const void *b) { const pid_t *p = a, *q = b; /* Suitable for usage in qsort() */ - - if (*p < *q) - return -1; - if (*p > *q) - return 1; - return 0; + return CMP(*p, *q); } int ioprio_parse_priority(const char *s, int *ret) { diff --git a/src/core/socket.c b/src/core/socket.c index aedbf51a4c..fb17b3be2d 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -484,11 +484,11 @@ static void peer_address_hash_func(const void *p, struct siphash *state) { static int peer_address_compare_func(const void *a, const void *b) { const SocketPeer *x = a, *y = b; + int r; - if (x->peer.sa.sa_family < y->peer.sa.sa_family) - return -1; - if (x->peer.sa.sa_family > y->peer.sa.sa_family) - return 1; + r = CMP(x->peer.sa.sa_family, y->peer.sa.sa_family); + if (r != 0) + return r; switch(x->peer.sa.sa_family) { case AF_INET: diff --git a/src/journal/catalog.c b/src/journal/catalog.c index f9118f0b62..4ae1136e86 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -59,12 +59,12 @@ static void catalog_hash_func(const void *p, struct siphash *state) { static int catalog_compare_func(const void *a, const void *b) { const CatalogItem *i = a, *j = b; unsigned k; + int r; for (k = 0; k < ELEMENTSOF(j->id.bytes); k++) { - if (i->id.bytes[k] < j->id.bytes[k]) - return -1; - if (i->id.bytes[k] > j->id.bytes[k]) - return 1; + r = CMP(i->id.bytes[k], j->id.bytes[k]); + if (r != 0) + return r; } return strcmp(i->language, j->language); diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c index 5dcb051373..8295d4d404 100644 --- a/src/libsystemd-network/lldp-neighbor.c +++ b/src/libsystemd-network/lldp-neighbor.c @@ -26,22 +26,15 @@ static int lldp_neighbor_id_compare_func(const void *a, const void *b) { if (r != 0) return r; - if (x->chassis_id_size < y->chassis_id_size) - return -1; - - if (x->chassis_id_size > y->chassis_id_size) - return 1; + r = CMP(x->chassis_id_size, y->chassis_id_size); + if (r != 0) + return r; r = memcmp(x->port_id, y->port_id, MIN(x->port_id_size, y->port_id_size)); if (r != 0) return r; - if (x->port_id_size < y->port_id_size) - return -1; - if (x->port_id_size > y->port_id_size) - return 1; - - return 0; + return CMP(x->port_id_size, y->port_id_size); } const struct hash_ops lldp_neighbor_id_hash_ops = { @@ -52,13 +45,7 @@ const struct hash_ops lldp_neighbor_id_hash_ops = { int lldp_neighbor_prioq_compare_func(const void *a, const void *b) { const sd_lldp_neighbor *x = a, *y = b; - if (x->until < y->until) - return -1; - - if (x->until > y->until) - return 1; - - return 0; + return CMP(x->until, y->until); } _public_ sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n) { diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index 5ca46b3502..d91849f379 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -125,6 +125,7 @@ void client_id_hash_func(const void *p, struct siphash *state) { int client_id_compare_func(const void *_a, const void *_b) { const DHCPClientId *a, *b; + int r; a = _a; b = _b; @@ -132,8 +133,9 @@ int client_id_compare_func(const void *_a, const void *_b) { assert(!a->length || a->data); assert(!b->length || b->data); - if (a->length != b->length) - return a->length < b->length ? -1 : 1; + r = CMP(a->length, b->length); + if (r != 0) + return r; return memcmp(a->data, b->data, a->length); } diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index 5bddf88e36..eae129b95c 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -144,19 +144,18 @@ static void address_hash_func(const void *b, struct siphash *state) { static int address_compare_func(const void *c1, const void *c2) { const Address *a1 = c1, *a2 = c2; + int r; - if (a1->family < a2->family) - return -1; - if (a1->family > a2->family) - return 1; + r = CMP(a1->family, a2->family); + if (r != 0) + return r; switch (a1->family) { /* use the same notion of equality as the kernel does */ case AF_INET: - if (a1->prefixlen < a2->prefixlen) - return -1; - if (a1->prefixlen > a2->prefixlen) - return 1; + r = CMP(a1->prefixlen, a2->prefixlen); + if (r != 0) + return r; /* compare the peer prefixes */ if (a1->prefixlen != 0) { @@ -174,10 +173,9 @@ static int address_compare_func(const void *c1, const void *c2) { else b2 = be32toh(a2->in_addr.in.s_addr) >> (32 - a1->prefixlen); - if (b1 < b2) - return -1; - if (b1 > b2) - return 1; + r = CMP(b1, b2); + if (r != 0) + return r; } _fallthrough_; diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c index b335fdb1bb..aa86ce792d 100644 --- a/src/network/networkd-route.c +++ b/src/network/networkd-route.c @@ -164,34 +164,30 @@ static void route_hash_func(const void *b, struct siphash *state) { static int route_compare_func(const void *_a, const void *_b) { const Route *a = _a, *b = _b; + int r; - if (a->family < b->family) - return -1; - if (a->family > b->family) - return 1; + r = CMP(a->family, b->family); + if (r != 0) + return r; switch (a->family) { case AF_INET: case AF_INET6: - if (a->dst_prefixlen < b->dst_prefixlen) - return -1; - if (a->dst_prefixlen > b->dst_prefixlen) - return 1; + r = CMP(a->dst_prefixlen, b->dst_prefixlen); + if (r != 0) + return r; - if (a->tos < b->tos) - return -1; - if (a->tos > b->tos) - return 1; + r = CMP(a->tos, b->tos); + if (r != 0) + return r; - if (a->priority < b->priority) - return -1; - if (a->priority > b->priority) - return 1; + r = CMP(a->priority, b->priority); + if (r != 0) + return r; - if (a->table < b->table) - return -1; - if (a->table > b->table) - return 1; + r = CMP(a->table, b->table); + if (r != 0) + return r; return memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family)); default: diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index 650ea0af52..dbb06a8074 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -92,38 +92,32 @@ static int routing_policy_rule_compare_func(const void *_a, const void *_b) { const RoutingPolicyRule *a = _a, *b = _b; int r; - if (a->family < b->family) - return -1; - if (a->family > b->family) - return 1; + r = CMP(a->family, b->family); + if (r != 0) + return r; switch (a->family) { case AF_INET: case AF_INET6: - if (a->from_prefixlen < b->from_prefixlen) - return -1; - if (a->from_prefixlen > b->from_prefixlen) - return 1; + r = CMP(a->from_prefixlen, b->from_prefixlen); + if (r != 0) + return r; - if (a->to_prefixlen < b->to_prefixlen) - return -1; - if (a->to_prefixlen > b->to_prefixlen) - return 1; + r = CMP(a->to_prefixlen, b->to_prefixlen); + if (r != 0) + return r; - if (a->tos < b->tos) - return -1; - if (a->tos > b->tos) - return 1; + r = CMP(a->tos, b->tos); + if (r != 0) + return r; - if (a->fwmask < b->fwmark) - return -1; - if (a->fwmask > b->fwmark) - return 1; + r = CMP(a->fwmask, b->fwmask); + if (r != 0) + return r; - if (a->table < b->table) - return -1; - if (a->table > b->table) - return 1; + r = CMP(a->table, b->table); + if (r != 0) + return r; r = strcmp_ptr(a->iif, b->iif); if (!r) diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index 403d23d4fb..95e9e072c1 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -228,11 +228,7 @@ void dns_cache_prune(DnsCache *c) { static int dns_cache_item_prioq_compare_func(const void *a, const void *b) { const DnsCacheItem *x = a, *y = b; - if (x->until < y->until) - return -1; - if (x->until > y->until) - return 1; - return 0; + return CMP(x->until, y->until); } static int dns_cache_init(DnsCache *c) { diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c index 097942ee0a..2cc606f363 100644 --- a/src/resolve/resolved-dns-packet.c +++ b/src/resolve/resolved-dns-packet.c @@ -2339,11 +2339,11 @@ static void dns_packet_hash_func(const void *p, struct siphash *state) { static int dns_packet_compare_func(const void *a, const void *b) { const DnsPacket *x = a, *y = b; + int r; - if (x->size < y->size) - return -1; - if (x->size > y->size) - return 1; + r = CMP(x->size, y->size); + if (r != 0) + return r; return memcmp(DNS_PACKET_DATA((DnsPacket*) x), DNS_PACKET_DATA((DnsPacket*) y), x->size); } diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index cfd0ed214d..05a4915101 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -300,15 +300,13 @@ static int dns_resource_key_compare_func(const void *a, const void *b) { if (ret != 0) return ret; - if (x->type < y->type) - return -1; - if (x->type > y->type) - return 1; + ret = CMP(x->type, y->type); + if (ret != 0) + return ret; - if (x->class < y->class) - return -1; - if (x->class > y->class) - return 1; + ret = CMP(x->class, y->class); + if (ret != 0) + return ret; return 0; } diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c index eff3dbb275..24164362c4 100644 --- a/src/resolve/resolved-dns-server.c +++ b/src/resolve/resolved-dns-server.c @@ -624,19 +624,17 @@ static int dns_server_compare_func(const void *a, const void *b) { const DnsServer *x = a, *y = b; int r; - if (x->family < y->family) - return -1; - if (x->family > y->family) - return 1; + r = CMP(x->family, y->family); + if (r != 0) + return r; r = memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family)); if (r != 0) return r; - if (x->ifindex < y->ifindex) - return -1; - if (x->ifindex > y->ifindex) - return 1; + r = CMP(x->ifindex, y->ifindex); + if (r != 0) + return r; return 0; }