From 1c76fe418b08e19718ca288a99db55605e5853e2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Oct 2022 11:17:49 +0200 Subject: [PATCH] glib-aux: use nm_assert() in nm_{ptr,}array_find_bsearch() These checks don't seem very useful, to have them enabled in production code. What is actually the real danger of messing up with binary search, is that the input array is not properly sorted. Asserting for that would be way more useful, but also likely too expensive to be worth it. Checking that the input arguments are not NULL/zero, is not that useful, because we "usually" won't make such mistakes. While at it, declare each local variable on a separate line. --- src/libnm-glib-aux/nm-shared-utils.c | 29 ++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c index 58c424fd34..f60ae1f2d6 100644 --- a/src/libnm-glib-aux/nm-shared-utils.c +++ b/src/libnm-glib-aux/nm-shared-utils.c @@ -3793,11 +3793,13 @@ nm_ptrarray_find_bsearch(gconstpointer *list, GCompareDataFunc cmpfcn, gpointer user_data) { - gssize imin, imax, imid; + gssize imax; + gssize imid; + gssize imin; int cmp; - g_return_val_if_fail(list || !len, ~((gssize) 0)); - g_return_val_if_fail(cmpfcn, ~((gssize) 0)); + nm_assert(list || len == 0); + nm_assert(cmpfcn); imin = 0; if (len > 0) { @@ -3832,11 +3834,16 @@ nm_ptrarray_find_bsearch_range(gconstpointer *list, gssize *out_idx_first, gssize *out_idx_last) { - gssize imin, imax, imid, i2min, i2max, i2mid; + gssize imax; + gssize imid; + gssize imin; + gssize i2max; + gssize i2mid; + gssize i2min; int cmp; - g_return_val_if_fail(list || !len, ~((gssize) 0)); - g_return_val_if_fail(cmpfcn, ~((gssize) 0)); + nm_assert(list || len == 0); + nm_assert(cmpfcn); imin = 0; if (len > 0) { @@ -3936,12 +3943,14 @@ nm_array_find_bsearch(gconstpointer list, GCompareDataFunc cmpfcn, gpointer user_data) { - gssize imin, imax, imid; + gssize imax; + gssize imid; + gssize imin; int cmp; - g_return_val_if_fail(list || !len, ~((gssize) 0)); - g_return_val_if_fail(cmpfcn, ~((gssize) 0)); - g_return_val_if_fail(elem_size > 0, ~((gssize) 0)); + nm_assert(list || len == 0); + nm_assert(cmpfcn); + nm_assert(elem_size > 0); imin = 0; if (len == 0)