diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-03-25 22:43:43 +0200 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2018-08-24 16:47:08 +0200 |
commit | cd5e383ccf904a09a60cd8bb73b42c2c19e99cac (patch) | |
tree | bba9a7f7dae2eb372b90cac1a2be0bb23e4b0f6b /src/basic | |
parent | 7c9bebfd22b33d026e66227d9b3a2b33d4a3265b (diff) |
tree-wide: add bsearch_safe and use where appropriate
Should fix #8557.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/util.h | 13 |
1 files changed, 13 insertions, 0 deletions
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 @@ -100,6 +100,19 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, #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. */ |