tree-wide: add bsearch_safe and use where appropriate

Should fix #8557.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-03-26 15:28:03 +02:00
parent 989290dbf1
commit d6c5d19b66
4 changed files with 21 additions and 10 deletions
+13
View File
@@ -91,6 +91,19 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
int (*compar) (const void *, const void *, void *),
void *arg);
/**
* Normal bsearch requires base to be nonnull. Here were require
* that only if nmemb > 0.
*/
static inline void* bsearch_safe(const void *key, const void *base,
size_t nmemb, size_t size, comparison_fn_t compar) {
if (nmemb <= 0)
return NULL;
assert(base);
return bsearch(key, base, nmemb, size, compar);
}
/**
* Normal qsort requires base to be nonnull. Here were require
* that only if nmemb > 0.