[sheepdog] [PATCH] improve type check of xbsearch

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Jun 7 07:32:25 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

It is possible that 'key' is a const variable and 'base' is not, or
vice versa.  This patches allows such cases.

This also makes xbsearch() return the type of 'base' instead of a void
pointer.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 include/util.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/include/util.h b/include/util.h
index 1c42ff8..8fb1db2 100644
--- a/include/util.h
+++ b/include/util.h
@@ -112,9 +112,14 @@ int atomic_create_and_write(const char *path, char *buf, size_t len);
 /* a type safe version of bsearch() */
 #define xbsearch(key, base, nmemb, compar)				\
 ({									\
-	(void) (key == base);						\
-	assert(compar(key, key) == 0);					\
-	bsearch(key, base, nmemb, sizeof(*(base)), (comparison_fn_t)compar); \
+	typeof(&(base)[0]) __ret = NULL;				\
+	if (nmemb > 0) {						\
+		assert(compar(key, key) == 0);				\
+		assert(compar(base, base) == 0);			\
+		__ret = bsearch(key, base, nmemb, sizeof(*(base)),	\
+				(comparison_fn_t)compar);		\
+	}								\
+	__ret;								\
 })
 
 #ifdef assert
-- 
1.7.9.5




More information about the sheepdog mailing list