[sheepdog] [PATCH] util: make xbsearch(), xlfind(), and xlremove() type safe

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Sep 2 05:08:58 CEST 2013


This patch adds BUILD_BUG_ON() checks to xbsearch(), xlfind() and
xlremove(). These checks can prevent wrong usage of these functions
(e.g. comparing sd_node and sd_vnode, removed in the commit 15e7d5fb).

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 include/util.h |    3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/util.h b/include/util.h
index ace763b..d4aaea3 100644
--- a/include/util.h
+++ b/include/util.h
@@ -134,6 +134,7 @@ 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)				\
 ({									\
+	BUILD_BUG_ON(sizeof(*key) != sizeof(base[0]));			\
 	typeof(&(base)[0]) __ret = NULL;				\
 	if (nmemb > 0) {						\
 		assert(compar(key, key) == 0);				\
@@ -147,6 +148,7 @@ int atomic_create_and_write(const char *path, char *buf, size_t len,
 /* a type safe version of lfind() */
 #define xlfind(key, base, nmemb, compar)				\
 ({									\
+	BUILD_BUG_ON(sizeof(*key) != sizeof(base[0]));			\
 	typeof(&(base)[0]) __ret = NULL;				\
 	if (nmemb > 0) {						\
 		size_t __n = nmemb;					\
@@ -169,6 +171,7 @@ int atomic_create_and_write(const char *path, char *buf, size_t len,
 	bool __removed = false;						\
 	typeof(&(base)[0]) __e;						\
 									\
+	BUILD_BUG_ON(sizeof(*key) != sizeof(base[0]));			\
 	__e = xlfind(key, base, *(nmemb), compar);			\
 	if (__e != NULL) {						\
 		(*(nmemb))--;						\
-- 
1.7.10.4




More information about the sheepdog mailing list