From: Arran Cudbard-Bell Date: Fri, 7 Jul 2017 01:09:23 +0000 (-0400) Subject: Reduce branches in comparators X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a18a41b65177ab6fbfec1fc5f84efed22b60ef1;p=thirdparty%2Ffreeradius-server.git Reduce branches in comparators --- diff --git a/src/lib/io/network.c b/src/lib/io/network.c index 72ec047318f..3a60a7c1e0d 100644 --- a/src/lib/io/network.c +++ b/src/lib/io/network.c @@ -95,38 +95,27 @@ static void fr_network_post_event(fr_event_list_t *el, struct timeval *now, void static int worker_cmp(void const *one, void const *two) { - fr_network_worker_t const *a = one; - fr_network_worker_t const *b = two; + fr_network_worker_t const *a = one, *b = two; - if (a->cpu_time < b->cpu_time) return -1; - if (a->cpu_time > b->cpu_time) return +1; - - return 0; + return (a->cpu_time < b->cpu_time) - (a->cpu_time > b->cpu_time); } static int reply_cmp(void const *one, void const *two) { - fr_channel_data_t const *a = one; - fr_channel_data_t const *b = two; - - if (a->priority < b->priority) return -1; - if (a->priority > b->priority) return +1; + fr_channel_data_t const *a = one, *b = two; + int ret; - if (a->m.when < b->m.when) return -1; - if (a->m.when > b->m.when) return +1; + ret = (a->priority > b->priority) - (a->priority < b->priority); + if (ret != 0) return ret; - return 0; + return (a->m.when > b->m.when) - (a->m.when < b->m.when); } static int socket_cmp(void const *one, void const *two) { - fr_network_socket_t const *a = one; - fr_network_socket_t const *b = two; - - if (a->listen < b->listen) return -1; - if (a->listen > b->listen) return +1; + fr_network_socket_t const *a = one, *b = two; - return 0; + return (a->listen > b->listen) - (a->listen < b->listen); } diff --git a/src/lib/io/track.c b/src/lib/io/track.c index c9dffa84712..1b05ac21c12 100644 --- a/src/lib/io/track.c +++ b/src/lib/io/track.c @@ -55,19 +55,15 @@ struct fr_tracking_t { static int entry_cmp(void const *one, void const *two) { - fr_tracking_entry_t const *a = one; - fr_tracking_entry_t const *b = two; + fr_tracking_entry_t const *a = one, *b = two; /* * Check Code and Identifier. * * But NOT the Request Authenticator. */ - if (a->data[0] < b->data[0]) return -1; - if (a->data[0] > b->data[0]) return +1; - - if (a->data[1] < b->data[1]) return -1; - if (a->data[1] > b->data[1]) return +1; + if (a->data[0] != b->data[0]) return a->data[0] - b->data[0]; + if (a->data[1] != b->data[1]) return a->data[1] - b->data[1]; return memcmp(a->src_dst, b->src_dst, a->src_dst_size); } diff --git a/src/lib/io/worker.c b/src/lib/io/worker.c index dd1c735b0ee..7d6c87415c3 100644 --- a/src/lib/io/worker.c +++ b/src/lib/io/worker.c @@ -941,16 +941,13 @@ static int fr_worker_pre_event(void *ctx, struct timeval *wake) */ static int worker_message_cmp(void const *one, void const *two) { - fr_channel_data_t const *a = one; - fr_channel_data_t const *b = two; + fr_channel_data_t const *a = one, *b = two; + int ret; - if (a->priority < b->priority) return -1; - if (a->priority > b->priority) return +1; + ret = (a->priority > b->priority) - (a->priority < b->priority); + if (ret != 0) return ret; - if (a->m.when < b->m.when) return -1; - if (a->m.when > b->m.when) return +1; - - return 0; + return (a->m.when > b->m.when) - (a->m.when < b->m.when); } /** @@ -958,16 +955,13 @@ static int worker_message_cmp(void const *one, void const *two) */ static int worker_request_cmp(void const *one, void const *two) { - REQUEST const *a = one; - REQUEST const *b = two; + REQUEST const *a = one, *b = two; + int ret; - if (a->async->priority < b->async->priority) return -1; - if (a->async->priority > b->async->priority) return +1; + ret = (a->async->priority > b->async->priority) - (a->async->priority < b->async->priority); + if (ret != 0) return ret; - if (a->async->recv_time < b->async->recv_time) return -1; - if (a->async->recv_time > b->async->recv_time) return +1; - - return 0; + return (a->async->recv_time > b->async->recv_time) - (a->async->recv_time < b->async->recv_time); } /** Destroy a worker. diff --git a/src/lib/util/dict.c b/src/lib/util/dict.c index dd7dcace694..b8ad6c44233 100644 --- a/src/lib/util/dict.c +++ b/src/lib/util/dict.c @@ -298,8 +298,7 @@ static uint32_t dict_attr_name_hash(void const *data) */ static int dict_attr_name_cmp(void const *one, void const *two) { - fr_dict_attr_t const *a = one; - fr_dict_attr_t const *b = two; + fr_dict_attr_t const *a = one, *b = two; return strcasecmp(a->name, b->name); } @@ -322,16 +321,16 @@ static uint32_t dict_attr_combo_hash(void const *data) */ static int dict_attr_combo_cmp(void const *one, void const *two) { - fr_dict_attr_t const *a = one; - fr_dict_attr_t const *b = two; + fr_dict_attr_t const *a = one, *b = two; + int ret; - if (a->parent < b->parent) return -1; - if (a->parent > b->parent) return +1; + ret = (a->parent < b->parent) - (a->parent > b->parent); + if (ret != 0) return ret; - if (a->type < b->type) return -1; - if (a->type > b->type) return +1; + ret = (a->type < b->type) - (a->type > b->type); + if (ret != 0) return ret; - return a->attr - b->attr; + return (a->attr > b->attr) - (a->attr < b->attr); } /** Wrap name hash function for fr_dict_vendor_t diff --git a/src/lib/util/event.c b/src/lib/util/event.c index c8cc531d92a..1dfc99cdcc1 100644 --- a/src/lib/util/event.c +++ b/src/lib/util/event.c @@ -149,8 +149,7 @@ struct fr_event_list_t { static int fr_event_timer_cmp(void const *a, void const *b) { int ret; - fr_event_timer_t const *ev_a = a; - fr_event_timer_t const *ev_b = b; + fr_event_timer_t const *ev_a = a, *ev_b = b; return (ret = ((ev_a->when.tv_sec < ev_b->when.tv_sec) - (ev_a->when.tv_sec > ev_b->when.tv_sec))) ? ret : @@ -168,8 +167,7 @@ static int fr_event_timer_cmp(void const *a, void const *b) */ static int fr_event_fd_cmp(void const *a, void const *b) { - fr_event_fd_t const *ev_a = a; - fr_event_fd_t const *ev_b = b; + fr_event_fd_t const *ev_a = a, *ev_b = b; return (ev_a->fd < ev_b->fd) - (ev_a->fd > ev_b->fd); } diff --git a/src/lib/util/heap.c b/src/lib/util/heap.c index 8e021f90f86..b2c905778a8 100644 --- a/src/lib/util/heap.c +++ b/src/lib/util/heap.c @@ -278,14 +278,9 @@ typedef struct heap_thing { */ static int heap_cmp(void const *one, void const *two) { - heap_thing const *a; - heap_thing const *b; - - a = (heap_thing const *) one; - b = (heap_thing const *) two; + heap_thing const *a = one, *b = two; return a->data - b->data; - } #define ARRAY_SIZE (1024) diff --git a/src/lib/util/inet.c b/src/lib/util/inet.c index 4aaed1df831..18967894d0e 100644 --- a/src/lib/util/inet.c +++ b/src/lib/util/inet.c @@ -1081,11 +1081,8 @@ int fr_ipaddr_from_ifindex(fr_ipaddr_t *out, int fd, int af, int if_index) */ int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b) { - if (a->af < b->af) return -1; - if (a->af > b->af) return +1; - - if (a->prefix < b->prefix) return -1; - if (a->prefix > b->prefix) return +1; + if (a->af != b->af) return a->af - b->af; + if (a->prefix != b->prefix) return a->prefix - b->prefix; switch (a->af) { case AF_INET: @@ -1095,12 +1092,8 @@ int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b) #ifdef HAVE_STRUCT_SOCKADDR_IN6 case AF_INET6: - if (a->scope_id < b->scope_id) return -1; - if (a->scope_id > b->scope_id) return +1; - - return memcmp(&a->addr.v6, - &b->addr.v6, - sizeof(a->addr.v6)); + if (a->scope_id != b->scope_id) return a->scope_id - b->scope_id; + return memcmp(&a->addr.v6, &b->addr.v6, sizeof(a->addr.v6)); #endif default: diff --git a/src/lib/util/misc.c b/src/lib/util/misc.c index 5f3a821799e..1bbfad4e6c8 100644 --- a/src/lib/util/misc.c +++ b/src/lib/util/misc.c @@ -1047,13 +1047,12 @@ void fr_timeval_divide(struct timeval *out, struct timeval const *in, int diviso */ int fr_timeval_cmp(struct timeval const *a, struct timeval const *b) { - if (a->tv_sec > b->tv_sec) return +1; - if (a->tv_sec < b->tv_sec) return -1; + int ret; - if (a->tv_usec > b->tv_usec) return +1; - if (a->tv_usec < b->tv_usec) return -1; + ret = (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec); + if (ret != 0) return ret; - return 0; + return (a->tv_usec > b->tv_usec) - (a->tv_usec < b->tv_usec); } /** Create timeval from a string @@ -1228,10 +1227,7 @@ bool fr_multiply(uint64_t *result, uint64_t lhs, uint64_t rhs) */ int8_t fr_pointer_cmp(void const *a, void const *b) { - if (a < b) return -1; - if (a == b) return 0; - - return 1; + return (a > b) - (a < b); } /** Quick sort an array of pointers using a comparator @@ -1289,9 +1285,7 @@ int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length) int result = 0; size_t i; - for (i = 0; i < length; i++) { - result |= a[i] ^ b[i]; - } + for (i = 0; i < length; i++) result |= a[i] ^ b[i]; return result; /* 0 is OK, !0 is !OK, just like memcmp */ } diff --git a/src/lib/util/pair.c b/src/lib/util/pair.c index 2fa6c981e86..dc58d138227 100644 --- a/src/lib/util/pair.c +++ b/src/lib/util/pair.c @@ -863,11 +863,7 @@ int8_t fr_pair_cmp_by_da_tag(void const *a, void const *b) cmp = fr_pointer_cmp(my_a->da, my_b->da); if (cmp != 0) return cmp; - if (my_a->tag < my_b->tag) return -1; - - if (my_a->tag > my_b->tag) return 1; - - return 0; + return (my_a->tag > my_b->tag) - (my_a->tag < my_b->tag); } /** Order attributes by their attribute number, and tag @@ -1042,29 +1038,20 @@ int fr_pair_list_cmp(VALUE_PAIR *a, VALUE_PAIR *b) { vp_cursor_t a_cursor, b_cursor; VALUE_PAIR *a_p, *b_p; - int ret; for (a_p = fr_pair_cursor_init(&a_cursor, &a), b_p = fr_pair_cursor_init(&b_cursor, &b); a_p && b_p; a_p = fr_pair_cursor_next(&a_cursor), b_p = fr_pair_cursor_next(&b_cursor)) { + int ret; + /* Same VP, no point doing expensive checks */ - if (a_p == b_p) { - continue; - } + if (a_p == b_p) continue; - if (a_p->da < b_p->da) { - return -1; - } - if (a_p->da > b_p->da) { - return 1; - } + ret = (a_p->da < b_p->da) - (a_p->da > b_p->da); + if (ret != 0) return ret; - if (a_p->tag < b_p->tag) { - return -1; - } - if (a_p->tag > b_p->tag) { - return 1; - } + ret = (a_p->tag < b_p->tag) - (a_p->tag > b_p->tag); + if (ret != 0) return ret; ret = fr_value_box_cmp(&a_p->data, &b_p->data); if (ret != 0) { diff --git a/src/main/cf_file.c b/src/main/cf_file.c index ee27b6c3e49..ee0fc7385bf 100644 --- a/src/main/cf_file.c +++ b/src/main/cf_file.c @@ -409,16 +409,13 @@ static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template) */ static int _filename_cmp(void const *a, void const *b) { - cf_file_t const *one = a; - cf_file_t const *two = b; + cf_file_t const *one = a, *two = b; + int ret; - if (one->buf.st_dev < two->buf.st_dev) return -1; - if (one->buf.st_dev > two->buf.st_dev) return +1; + ret = (one->buf.st_dev < two->buf.st_dev) - (one->buf.st_dev > two->buf.st_dev); + if (ret != 0) return ret; - if (one->buf.st_ino < two->buf.st_ino) return -1; - if (one->buf.st_ino > two->buf.st_ino) return +1; - - return 0; + return (one->buf.st_ino < two->buf.st_ino) - (one->buf.st_ino > two->buf.st_ino); } static FILE *cf_file_open(CONF_SECTION *cs, char const *filename) diff --git a/src/main/client.c b/src/main/client.c index a88f343766a..ab9bf6556e4 100644 --- a/src/main/client.c +++ b/src/main/client.c @@ -141,8 +141,7 @@ static int client_ipaddr_cmp(void const *one, void const *two) */ static int client_num_cmp(void const *one, void const *two) { - RADCLIENT const *a = one; - RADCLIENT const *b = two; + RADCLIENT const *a = one, *b = two; return (a->number - b->number); } diff --git a/src/main/cond_eval.c b/src/main/cond_eval.c index d530e93134a..d69209529d2 100644 --- a/src/main/cond_eval.c +++ b/src/main/cond_eval.c @@ -372,7 +372,7 @@ static int cond_normalise_and_cmp(REQUEST *request, fr_cond_t const *c, fr_value fr_value_box_t *rhs = NULL; fr_dict_attr_t const *cast = NULL; - fr_type_t cast_type = FR_TYPE_INVALID; + fr_type_t cast_type = FR_TYPE_INVALID; fr_value_box_t lhs_cast, rhs_cast; void *lhs_cast_buff = NULL, *rhs_cast_buff = NULL; diff --git a/src/main/dl.c b/src/main/dl.c index 0b833e1e1b8..85725a92339 100644 --- a/src/main/dl.c +++ b/src/main/dl.c @@ -119,47 +119,45 @@ static int dl_init(void); static int dl_symbol_init_cmp(void const *one, void const *two) { - dl_symbol_init_t const *a = one; - dl_symbol_init_t const *b = two; + dl_symbol_init_t const *a = one, *b = two; + int ret; rad_assert(a && b); - if ((void *)a->func > (void *)b->func) return +1; - if ((void *)a->func < (void *)b->func) return -1; + ret = ((void *)a->func > (void *)b->func) - ((void *)a->func < (void *)b->func); + if (ret != 0) return ret; - if (a->symbol && !b->symbol) return +1; - if (!a->symbol && b->symbol) return -1; - if (a->symbol && b->symbol) return strcmp(a->symbol, b->symbol); + ret = (a->symbol && !b->symbol) - (!a->symbol && b->symbol); + if (ret != 0) return ret; - return 0; + if (!a->symbol && !b->symbol) return 0; + + return strcmp(a->symbol, b->symbol); } static int dl_symbol_free_cmp(void const *one, void const *two) { - dl_symbol_free_t const *a = one; - dl_symbol_free_t const *b = two; + dl_symbol_free_t const *a = one, *b = two; + int ret; rad_assert(a && b); - if ((void *)a->func > (void *)b->func) return +1; - if ((void *)a->func < (void *)b->func) return -1; + ret = ((void *)a->func > (void *)b->func) - ((void *)a->func < (void *)b->func); + if (ret != 0) return ret; - if (a->symbol && !b->symbol) return +1; - if (!a->symbol && b->symbol) return -1; - if (a->symbol && b->symbol) return strcmp(a->symbol, b->symbol); + ret = (a->symbol && !b->symbol) - (!a->symbol && b->symbol); + if (ret != 0) return ret; - return 0; + if (!a->symbol && !b->symbol) return 0; + + return strcmp(a->symbol, b->symbol); } static int dl_inst_cmp(void const *one, void const *two) { - dl_instance_t const *a = one; - dl_instance_t const *b = two; - - if (a->data > b->data) return +1; - if (a->data < b->data) return -1; + dl_instance_t const *a = one, *b = two; - return 0; + return (a->data > b->data) - (a->data < b->data); } /** Compare the name of two dl_t diff --git a/src/main/listen.c b/src/main/listen.c index 0e0819e8008..b96a7625988 100644 --- a/src/main/listen.c +++ b/src/main/listen.c @@ -1189,9 +1189,7 @@ static CONF_PARSER limit_config[] = { */ static int listener_cmp(void const *one, void const *two) { - if (one < two) return -1; - if (one > two) return +1; - return 0; + return (one < two) - (one > two); } static int listener_unlink(UNUSED void *ctx, UNUSED void *data) diff --git a/src/main/map_proc.c b/src/main/map_proc.c index b8f23e2e7cb..b36ab5bf95e 100644 --- a/src/main/map_proc.c +++ b/src/main/map_proc.c @@ -61,8 +61,7 @@ struct map_proc_inst { */ static int map_proc_cmp(void const *one, void const *two) { - map_proc_t const *a = one; - map_proc_t const *b = two; + map_proc_t const *a = one, *b = two; if (a->length != b->length) return a->length - b->length; diff --git a/src/main/modules.c b/src/main/modules.c index 81bb171c360..efd441a1d48 100644 --- a/src/main/modules.c +++ b/src/main/modules.c @@ -539,10 +539,7 @@ static int _module_thread_inst_tree_cmp(void const *a, void const *b) { module_thread_instance_t const *my_a = a, *my_b = b; - if (my_a->inst > my_b->inst) return +1; - if (my_a->inst < my_b->inst) return -1; - - return 0; + return (my_a->inst > my_b->inst) - (my_a->inst < my_b->inst); } typedef struct { diff --git a/src/main/pool.c b/src/main/pool.c index 01e3101d656..6b5f3f84d6b 100644 --- a/src/main/pool.c +++ b/src/main/pool.c @@ -165,32 +165,29 @@ static const CONF_PARSER pool_config[] = { */ static int last_reserved_cmp(void const *one, void const *two) { - fr_pool_connection_t const *a = one; - fr_pool_connection_t const *b = two; + fr_pool_connection_t const *a = one, *b = two; + int ret; - if (a->last_reserved.tv_sec < b->last_reserved.tv_sec) return -1; - if (a->last_reserved.tv_sec > b->last_reserved.tv_sec) return +1; + ret = (a->last_reserved.tv_sec < b->last_reserved.tv_sec) - (a->last_reserved.tv_sec > b->last_reserved.tv_sec); + if (ret != 0) return ret; - if (a->last_reserved.tv_usec < b->last_reserved.tv_usec) return -1; - if (a->last_reserved.tv_usec > b->last_reserved.tv_usec) return +1; - - return 0; + return (a->last_reserved.tv_usec < b->last_reserved.tv_usec) - + (a->last_reserved.tv_usec > b->last_reserved.tv_usec); } /** Order connections by released longest ago */ static int last_released_cmp(void const *one, void const *two) { - fr_pool_connection_t const *a = one; - fr_pool_connection_t const *b = two; + fr_pool_connection_t const *a = one, *b = two; + int ret; - if (b->last_released.tv_sec < a->last_released.tv_sec) return -1; - if (b->last_released.tv_sec > a->last_released.tv_sec) return +1; + ret = (b->last_released.tv_sec < a->last_released.tv_sec) - + (b->last_released.tv_sec > a->last_released.tv_sec); + if (ret != 0) return ret; - if (b->last_released.tv_usec < a->last_released.tv_usec) return -1; - if (b->last_released.tv_usec > a->last_released.tv_usec) return +1; - - return 0; + return (b->last_released.tv_usec < a->last_released.tv_usec) - + (b->last_released.tv_usec > a->last_released.tv_usec); } /** Removes a connection from the connection list diff --git a/src/main/radclient.c b/src/main/radclient.c index 98f3484bdce..253b82620e2 100644 --- a/src/main/radclient.c +++ b/src/main/radclient.c @@ -751,11 +751,9 @@ static int radclient_sane(rc_request_t *request) */ static int filename_cmp(void const *one, void const *two) { + rc_file_pair_t const *a = one, *b = two; int cmp; - rc_file_pair_t const *a = one; - rc_file_pair_t const *b = two; - cmp = strcmp(a->packets, b->packets); if (cmp != 0) return cmp; diff --git a/src/main/state.c b/src/main/state.c index 5ebab52bd1c..7a9c36a24d0 100644 --- a/src/main/state.c +++ b/src/main/state.c @@ -130,8 +130,7 @@ static void state_entry_unlink(fr_state_tree_t *state, fr_state_entry_t *entry); */ static int state_entry_cmp(void const *one, void const *two) { - fr_state_entry_t const *a = one; - fr_state_entry_t const *b = two; + fr_state_entry_t const *a = one, *b = two; return memcmp(a->state, b->state, sizeof(a->state)); } diff --git a/src/main/threads.c b/src/main/threads.c index be677e18179..1d288f5bd2c 100644 --- a/src/main/threads.c +++ b/src/main/threads.c @@ -716,11 +716,11 @@ static int pid_cmp(void const *one, void const *two) */ static int default_cmp(void const *one, void const *two) { - REQUEST const *a = one; - REQUEST const *b = two; + REQUEST const *a = one, *b = two; + int ret; - if (a->priority < b->priority) return -1; - if (a->priority > b->priority) return +1; + ret = (a->priority < b->priority) - (a->priority > b->priority); + if (ret != 0) return ret; return timestamp_cmp(one, two); } @@ -731,14 +731,14 @@ static int default_cmp(void const *one, void const *two) */ static int state_cmp(void const *one, void const *two) { - REQUEST const *a = one; - REQUEST const *b = two; + REQUEST const *a = one, *b = two; + int ret; /* * Rounds which are further along go higher in the heap. */ - if (a->packet->rounds > b->packet->rounds) return -1; - if (a->packet->rounds < b->packet->rounds) return +1; + ret = (a->packet->rounds > b->packet->rounds) - (a->packet->rounds < b->packet->rounds); + if (ret != 0) return ret; return default_cmp(one, two); } diff --git a/src/main/trigger.c b/src/main/trigger.c index 7542e2a8b34..43c92a548a4 100644 --- a/src/main/trigger.c +++ b/src/main/trigger.c @@ -107,10 +107,7 @@ static int _trigger_last_fired_cmp(void const *a, void const *b) { trigger_last_fired_t const *lf_a = a, *lf_b = b; - if (lf_a->ci < lf_b->ci) return -1; - if (lf_a->ci == lf_b->ci) return 0; - - return 1; + return (lf_a->ci < lf_b->ci) - (lf_a->ci > lf_b->ci); } /** Set the global trigger section trigger_exec will search in, and register xlats diff --git a/src/main/xlat_func.c b/src/main/xlat_func.c index 1e3bc68fcfa..3c92143f301 100644 --- a/src/main/xlat_func.c +++ b/src/main/xlat_func.c @@ -649,10 +649,11 @@ done: */ static int xlat_cmp(void const *one, void const *two) { - xlat_t const *a = one; - xlat_t const *b = two; + xlat_t const *a = one, *b = two; + int ret; - if (a->length != b->length) return a->length - b->length; + ret = (a->length > b->length) - (a->length < b->length); + if (ret != 0) return ret; return memcmp(a->name, b->name, a->length); } diff --git a/src/modules/proto_bfd/proto_bfd.c b/src/modules/proto_bfd/proto_bfd.c index 686221f9a2a..de4fce8635e 100644 --- a/src/modules/proto_bfd/proto_bfd.c +++ b/src/modules/proto_bfd/proto_bfd.c @@ -1636,8 +1636,7 @@ static int bfd_socket_decode(UNUSED rad_listen_t *listener, UNUSED REQUEST *requ static int bfd_session_cmp(const void *one, const void *two) { - const bfd_state_t *a = one; - const bfd_state_t *b = two; + const bfd_state_t *a = one, *b = two; return fr_ipaddr_cmp(&a->remote_ipaddr, &b->remote_ipaddr); } diff --git a/src/modules/proto_ldap_sync/sync.c b/src/modules/proto_ldap_sync/sync.c index 7a7f061017f..27a1929bf47 100644 --- a/src/modules/proto_ldap_sync/sync.c +++ b/src/modules/proto_ldap_sync/sync.c @@ -877,8 +877,7 @@ static int _sync_state_free(sync_state_t *sync) */ static int _sync_cmp(void const *one, void const *two) { - sync_state_t const *a = one; - sync_state_t const *b = two; + sync_state_t const *a = one, *b = two; return a->msgid - b->msgid; } diff --git a/src/modules/rlm_cache/drivers/rlm_cache_rbtree/rlm_cache_rbtree.c b/src/modules/rlm_cache/drivers/rlm_cache_rbtree/rlm_cache_rbtree.c index 36a53f30c50..052a66f72fb 100644 --- a/src/modules/rlm_cache/drivers/rlm_cache_rbtree/rlm_cache_rbtree.c +++ b/src/modules/rlm_cache/drivers/rlm_cache_rbtree/rlm_cache_rbtree.c @@ -44,11 +44,11 @@ typedef struct rlm_cache_rbtree_entry { */ static int cache_entry_cmp(void const *one, void const *two) { - rlm_cache_entry_t const *a = one; - rlm_cache_entry_t const *b = two; + rlm_cache_entry_t const *a = one, *b = two; + int ret; - if (a->key_len < b->key_len) return -1; - if (a->key_len > b->key_len) return +1; + ret = (a->key_len > b->key_len) - (a->key_len < b->key_len); + if (ret != 0) return ret; return memcmp(a->key, b->key, a->key_len); } @@ -59,13 +59,9 @@ static int cache_entry_cmp(void const *one, void const *two) */ static int cache_heap_cmp(void const *one, void const *two) { - rlm_cache_entry_t const *a = one; - rlm_cache_entry_t const *b = two; - - if (a->expires < b->expires) return -1; - if (a->expires > b->expires) return +1; + rlm_cache_entry_t const *a = one, *b = two; - return 0; + return (a->expires > b->expires) - (a->expires < b->expires); } /** Walk over the cache rbtree diff --git a/src/modules/rlm_detail/rlm_detail.c b/src/modules/rlm_detail/rlm_detail.c index 450f85d7943..fd86d31cf0a 100644 --- a/src/modules/rlm_detail/rlm_detail.c +++ b/src/modules/rlm_detail/rlm_detail.c @@ -106,10 +106,7 @@ static uint32_t detail_hash(void const *data) static int detail_cmp(void const *a, void const *b) { - fr_dict_attr_t const *one = a; - fr_dict_attr_t const *two = b; - - return one - two; + return (a < b) - (a > b); } /* diff --git a/src/modules/rlm_files/rlm_files.c b/src/modules/rlm_files/rlm_files.c index ff2edc0fd69..31242eea076 100644 --- a/src/modules/rlm_files/rlm_files.c +++ b/src/modules/rlm_files/rlm_files.c @@ -93,8 +93,7 @@ static const CONF_PARSER module_config[] = { static int pairlist_cmp(void const *a, void const *b) { - return strcmp(((PAIR_LIST const *)a)->name, - ((PAIR_LIST const *)b)->name); + return strcmp(((PAIR_LIST const *)a)->name, ((PAIR_LIST const *)b)->name); } static int getusersfile(TALLOC_CTX *ctx, char const *filename, rbtree_t **ptree) diff --git a/src/modules/rlm_python/rlm_python.c b/src/modules/rlm_python/rlm_python.c index c984b1f2358..629be823710 100644 --- a/src/modules/rlm_python/rlm_python.c +++ b/src/modules/rlm_python/rlm_python.c @@ -642,9 +642,7 @@ static int _python_inst_cmp(const void *a, const void *b) { python_thread_state_t const *a_p = a, *b_p = b; - if (a_p->inst < b_p->inst) return -1; - if (a_p->inst > b_p->inst) return +1; - return 0; + return (a_p->inst < b_p->inst) - (a_p->inst > b_p->inst); } /** Thread safe call to a python function diff --git a/src/modules/rlm_redis/cluster.c b/src/modules/rlm_redis/cluster.c index 5193917c2ac..ec1954bf2d8 100644 --- a/src/modules/rlm_redis/cluster.c +++ b/src/modules/rlm_redis/cluster.c @@ -305,18 +305,13 @@ static uint16_t cluster_key_hash(uint8_t const *key, size_t key_len) */ static int _cluster_node_cmp(void const *a, void const *b) { + cluster_node_t const *my_a = a, *my_b = b; int ret; - cluster_node_t const *my_a = a; - cluster_node_t const *my_b = b; - ret = fr_ipaddr_cmp(&my_a->addr.ipaddr, &my_b->addr.ipaddr); if (ret != 0) return ret; - if (my_a->addr.port < my_b->addr.port) return -1; - if (my_a->addr.port > my_b->addr.port) return +1; - - return 0; + return my_a->addr.port - my_b->addr.port; } /** Reconnect callback to apply new pool config diff --git a/src/modules/rlm_redis_ippool/rlm_redis_ippool_tool.c b/src/modules/rlm_redis_ippool/rlm_redis_ippool_tool.c index 2309f4705f6..a42c35c8dbe 100644 --- a/src/modules/rlm_redis_ippool/rlm_redis_ippool_tool.c +++ b/src/modules/rlm_redis_ippool/rlm_redis_ippool_tool.c @@ -834,20 +834,16 @@ static int8_t pool_cmp(void const *a, void const *b) { size_t len_a; size_t len_b; - int ret; len_a = talloc_array_length((uint8_t const *)a); len_b = talloc_array_length((uint8_t const *)b); - if (len_a > len_b) return 1; - if (len_a < len_b) return -1; + ret = (len_a > len_b) - (len_a < len_b); + if (ret != 0) return ret; ret = memcmp(a, b, len_a); - if (ret > 0) return 1; - if (ret < 0) return -1; - - return 0; + return (ret > 0) - (ret < 0); } /** Return the pools available across the cluster diff --git a/src/protocols/dhcpv4/base.c b/src/protocols/dhcpv4/base.c index 0acc44c25eb..bf5e30a0dff 100644 --- a/src/protocols/dhcpv4/base.c +++ b/src/protocols/dhcpv4/base.c @@ -104,8 +104,7 @@ fr_dict_attr_t const *dhcp_option_82; int8_t fr_dhcpv4_attr_cmp(void const *a, void const *b) { - VALUE_PAIR const *my_a = a; - VALUE_PAIR const *my_b = b; + VALUE_PAIR const *my_a = a, *my_b = b; fr_dict_attr_t const *a_82, *b_82; VERIFY_VP(my_a);