lib: fix coverity #1641463

Use asserts and casts to inform SA to clear warning. Document valid return
values as well.

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2025-12-06 12:21:31 +00:00
parent abc53463f4
commit d7cc191bc1
2 changed files with 8 additions and 4 deletions

View file

@ -188,6 +188,7 @@ int _darr_search_floor(const void *a, size_t esize, const void *key, bool *equal
if (!cmp) {
if (equal)
*equal = true;
assert(mid >= -1);
return mid;
} else if (cmp < 0) {
floor = mid;
@ -197,6 +198,7 @@ int _darr_search_floor(const void *a, size_t esize, const void *key, bool *equal
}
}
assert(floor >= -1);
return floor;
#undef _a_at
}
@ -215,12 +217,13 @@ int _darr_search(const void *a, size_t esize, const void *key, darr_search_cmpf
uint _darr_search_ceil(const void *a, size_t esize, const void *key, bool *equal,
darr_search_cmpf cmpf)
{
uint i;
int i;
i = _darr_search_floor(a, esize, key, equal, cmpf);
assert(i >= -1);
if (*equal)
return i;
return i + 1;
return (uint)i;
return (uint)(i + 1);
}
int darr_strings_cmp(const char **a, const char *key)

View file

@ -872,7 +872,8 @@ extern int _darr_search_floor(const void *a, size_t esize, const void *key, bool
* The index of the greatest element that is less than or equal to the @K
* string. @E is set to true if equal otherwise false. If used with
* darr_insert() then the index should be passed +1 because darr_insert()
* inserts *before* the given index.
* inserts *before* the given index. If no element is less than or equal to
* the key then -1 is returned.
*/
#define darr_str_search_floor(A, K, E) \
_darr_search_floor((A), _darr_esize(A), (K), (E), (darr_search_cmpf)darr_strings_cmp)