* [PATCH] trace-cmd: Use builtin for offset_of
@ 2022-03-09 4:38 Ian Rogers
2022-03-09 16:39 ` Steven Rostedt
0 siblings, 1 reply; 2+ messages in thread
From: Ian Rogers @ 2022-03-09 4:38 UTC (permalink / raw)
To: linux-trace-devel, Tzvetomir Stoyanov, Steven Rostedt; +Cc: Ian Rogers
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/a0a26308-a1d7-a57c-727c-000652a5d246@fb.com/
Signed-off-by: Ian Rogers <irogers@google.com>
---
tracecmd/include/list.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tracecmd/include/list.h b/tracecmd/include/list.h
index cf9dd47e..fa0de6df 100644
--- a/tracecmd/include/list.h
+++ b/tracecmd/include/list.h
@@ -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 {
--
2.35.1.616.g0bdcbb4464-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] trace-cmd: Use builtin for offset_of
2022-03-09 4:38 [PATCH] trace-cmd: Use builtin for offset_of Ian Rogers
@ 2022-03-09 16:39 ` Steven Rostedt
0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2022-03-09 16:39 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-trace-devel, Tzvetomir Stoyanov
On Tue, 8 Mar 2022 20:38:15 -0800
Ian Rogers <irogers@google.com> 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/a0a26308-a1d7-a57c-727c-000652a5d246@fb.com/
Applied, thanks Ian!
-- Steve
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> tracecmd/include/list.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tracecmd/include/list.h b/tracecmd/include/list.h
> index cf9dd47e..fa0de6df 100644
> --- a/tracecmd/include/list.h
> +++ b/tracecmd/include/list.h
> @@ -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 {
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-03-09 16:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-09 4:38 [PATCH] trace-cmd: Use builtin for offset_of Ian Rogers
2022-03-09 16:39 ` Steven Rostedt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).