Since the comparison function cmp() has already been called with the correct parameters in the loop, it is enough to check if the assignment "left_idx = idx+1" has been executed. The special case "nmemb==0" is also handled well by this trick. --- src/lib/bsearch-insert-pos.c | 7 ++----- 1 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/lib/bsearch-insert-pos.c b/src/lib/bsearch-insert-pos.c index 7e5f8ae..8e63cf2 100644 --- a/src/lib/bsearch-insert-pos.c +++ b/src/lib/bsearch-insert-pos.c @@ -28,11 +28,8 @@ bool bsearch_insert_pos(const void *key, const void *base, unsigned int nmemb, } } - if (idx < nmemb) { - p = CONST_PTR_OFFSET(base, idx * size); - if (cmp(key, p) > 0) - ++idx; - } + if (left_idx > idx) + ++idx; *idx_r = idx; return FALSE;