From b1b864b398b2b7bc2ca54aca91b7abb1acee321d Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sat, 23 Jan 2021 00:55:25 -0500 Subject: [PATCH] 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 --- lib/ext2fs/compiler.h | 22 ++++++++++++++++++++++ lib/ext2fs/kernel-list.h | 4 +++- lib/ext2fs/rbtree.h | 12 +----------- 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 lib/ext2fs/compiler.h diff --git a/lib/ext2fs/compiler.h b/lib/ext2fs/compiler.h new file mode 100644 index 0000000..9aa9b4e --- /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 01f4f6b..dd7b8e0 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 9e80677..dfeeb23 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 #include - -#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 { -- 1.8.3.1