Skip to contentSkip to conversation

trace-cmd: Use builtin for offset_of

PATCH Applied Ian Rogers 2 participants 2 messages Mar 9, 2022

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 trace-cmd: Use builtin for offset_of

Arithmetic on null pointers is undefined behavior, spotted by C compiler sanitizers such as with clang. The builtin offset of is available in all recent compilers, so unconditionally use it as discussed for libbpf in:
https://lore.kernel.org/all/[email protected]/

---
 tracecmd/include/list.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tracecmd/include/list.h +1 -1
index cf9dd47e..fa0de6df 100644
@@ -6,7 +6,7 @@
 #ifndef __LIST_H
 #define __LIST_H
 
-#define offset_of(type, field)		(long)(&((type *)0)->field)
+#define offset_of(type, field)		__builtin_offsetof(type, field)
 #define container_of(p, type, field)	(type *)((long)p - offset_of(type, field))
 
 struct list_head {
Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
#1 lore raw
Quoted 4 lines · On Tue, 8 Mar 2022 20:38:15 -0800 Ian Rogers <[email protected]> wrote:

Arithmetic on null pointers is undefined behavior, spotted by C compiler sanitizers such as with clang. The builtin offset of is available in all recent compilers, so unconditionally use it as discussed for libbpf in:
https://lore.kernel.org/all/[email protected]/

Applied, thanks Ian!

-- Steve

Quoted 19 lines
---
 tracecmd/include/list.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tracecmd/include/list.h +1 -1
index cf9dd47e..fa0de6df 100644
@@ -6,7 +6,7 @@
 #ifndef __LIST_H
 #define __LIST_H
 
-#define offset_of(type, field)		(long)(&((type *)0)->field)
+#define offset_of(type, field)		__builtin_offsetof(type, field)
 #define container_of(p, type, field)	(type *)((long)p - offset_of(type, field))
 
 struct list_head {