[PATCH 1/3] tools/nolibc: use __builtin_offsetof()

Thomas Weißschuh posted 3 patches 5 hours ago
[PATCH 1/3] tools/nolibc: use __builtin_offsetof()
Posted by Thomas Weißschuh 5 hours ago
The current custom implementation of offsetof() fails UBSAN:
runtime error: member access within null pointer of type 'struct ...'
This means that all its users, including container_of(), free() and
realloc(), fail.

Use __builtin_offsetof() instead which does not have this issue and
has been available since GCC 4 and clang 4.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/include/nolibc/stddef.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/nolibc/stddef.h b/tools/include/nolibc/stddef.h
index ecbd13eab1f5..a3976341afdd 100644
--- a/tools/include/nolibc/stddef.h
+++ b/tools/include/nolibc/stddef.h
@@ -18,7 +18,7 @@
 #endif
 
 #ifndef offsetof
-#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
+#define offsetof(TYPE, FIELD) __builtin_offsetof(TYPE, FIELD)
 #endif
 
 #endif /* _NOLIBC_STDDEF_H */

-- 
2.53.0