]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
tools/nolibc: use __builtin_offsetof()
authorThomas Weißschuh <linux@weissschuh.net>
Wed, 1 Apr 2026 15:07:27 +0000 (17:07 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Sat, 4 Apr 2026 08:48:14 +0000 (10:48 +0200)
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 3.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260401-nolibc-asprintf-v1-1-46292313439f@weissschuh.net
tools/include/nolibc/stddef.h

index ecbd13eab1f5190fd0e90a07699c2f06dbde8916..a3976341afdd79605f8d77a86ea570483da86a92 100644 (file)
@@ -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 */