Whamcloud - gitweb
libext2fs: use compiler built-in offsetof() if available
authorTheodore Ts'o <tytso@mit.edu>
Sat, 23 Jan 2021 05:55:25 +0000 (00:55 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Sat, 23 Jan 2021 05:55:25 +0000 (00:55 -0500)
This avoids UBSAN sanitizer warnings, since &(0->member) is
technically undefined per the C standard.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/compiler.h [new file with mode: 0644]
lib/ext2fs/kernel-list.h
lib/ext2fs/rbtree.h

diff --git a/lib/ext2fs/compiler.h b/lib/ext2fs/compiler.h
new file mode 100644 (file)
index 0000000..9aa9b4e
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef _EXT2FS_COMPILER_H
+#define _EXT2FS_COMPILER_H
+
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
+#undef offsetof
+#if __has_builtin(__builtin_offsetof)
+#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
+#elif defined(__compiler_offsetof)
+#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
+#else
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
+
+#define container_of(ptr, type, member) ({                     \
+       const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
+       (type *)( (char *)__mptr - offsetof(type,member) );})
+
+
+#endif /* _EXT2FS_COMPILER_H */
index 01f4f6b..dd7b8e0 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef _LINUX_LIST_H
 #define _LINUX_LIST_H
 
+#include "compiler.h"
+
 /*
  * Simple doubly linked list implementation.
  *
@@ -101,7 +103,7 @@ static __inline__ void list_splice(struct list_head *list, struct list_head *hea
 }
 
 #define list_entry(ptr, type, member) \
-       ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
+       container_of(ptr, type, member)
 
 #define list_for_each(pos, head) \
         for (pos = (head)->next; pos != (head); pos = pos->next)
index 9e80677..dfeeb23 100644 (file)
@@ -96,17 +96,7 @@ static inline struct page * rb_insert_page_cache(struct inode * inode,
 
 #include <stdlib.h>
 #include <stdint.h>
-
-#undef offsetof
-#ifdef __compiler_offsetof
-#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
-#else
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
-
-#define container_of(ptr, type, member) ({                     \
-       const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
-       (type *)( (char *)__mptr - offsetof(type,member) );})
+#include "compiler.h"
 
 struct rb_node
 {