diff options
author | Theodore Ts'o <tytso@mit.edu> | 2021-01-23 00:55:25 -0500 |
---|---|---|
committer | Theodore Ts'o <tytso@mit.edu> | 2021-01-23 00:55:25 -0500 |
commit | b1b864b398b2b7bc2ca54aca91b7abb1acee321d (patch) | |
tree | 153393a2e02350fefb295374b025e4aa7d78241d | |
parent | c3c41d4ffbdbbce2e7199ece8f76cea0310de820 (diff) | |
download | e2fsprogs-b1b864b398b2b7bc2ca54aca91b7abb1acee321d.tar.gz |
libext2fs: use compiler built-in offsetof() if available
This avoids UBSAN sanitizer warnings, since &(0->member) is
technically undefined per the C standard.
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
-rw-r--r-- | lib/ext2fs/compiler.h | 22 | ||||
-rw-r--r-- | lib/ext2fs/kernel-list.h | 4 | ||||
-rw-r--r-- | lib/ext2fs/rbtree.h | 12 |
3 files changed, 26 insertions, 12 deletions
diff --git a/lib/ext2fs/compiler.h b/lib/ext2fs/compiler.h new file mode 100644 index 000000000..9aa9b4ec6 --- /dev/null +++ b/lib/ext2fs/compiler.h @@ -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 */ diff --git a/lib/ext2fs/kernel-list.h b/lib/ext2fs/kernel-list.h index 01f4f6b95..dd7b8e07d 100644 --- a/lib/ext2fs/kernel-list.h +++ b/lib/ext2fs/kernel-list.h @@ -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) diff --git a/lib/ext2fs/rbtree.h b/lib/ext2fs/rbtree.h index 9e8067799..dfeeb2342 100644 --- a/lib/ext2fs/rbtree.h +++ b/lib/ext2fs/rbtree.h @@ -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 { |