From cd5e383ccf904a09a60cd8bb73b42c2c19e99cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 25 Mar 2018 22:43:43 +0200 Subject: tree-wide: add bsearch_safe and use where appropriate Should fix #8557. --- src/basic/util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/basic') diff --git a/src/basic/util.h b/src/basic/util.h index c24fd6c63..6ffc54f98 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -99,6 +99,19 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, void *arg); #endif // 0 +/** + * 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. -- cgit v1.2.3