From: Ross Zwisler <zwisler@google.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Linux Trace Devel <linux-trace-devel@vger.kernel.org>,
Stevie Alvarez <stevie.6strings@gmail.com>
Subject: Re: [PATCH v2] libtraceeval: Have TRACEEVAL_ARRAY_SIZE() handle NULL pointer
Date: Tue, 17 Oct 2023 08:47:56 -0600 [thread overview]
Message-ID: <20231017144756.GA3977037@google.com> (raw)
In-Reply-To: <20231011191406.5c93f6ae@gandalf.local.home>
On Wed, Oct 11, 2023 at 07:14:06PM -0400, Steven Rostedt wrote:
> On Wed, 11 Oct 2023 16:33:48 -0600
> Ross Zwisler <zwisler@google.com> wrote:
>
> > On Thu, Oct 05, 2023 at 09:22:33PM -0400, Steven Rostedt wrote:
> > > From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> > >
> > > In the new addition to make sure that pointers passed to traceeval_init()
> > > and other functions that require a static array and not a dynamic one will
> > > cause the build to fail, this causes NULL pointers to fail the build too.
> > >
> > > Although keys must be filled, vals are allowed to be NULL. It was assumed
> > > that:
> > >
> > > (void *)vals == NULL ? TRACEEVAL_ARRAY_SIZE(vals))
> > >
> > > Would solve this, but it gcc was actually still giving a warning about
> > >
> > > warning: division 'sizeof (void *) / sizeof (void)' does not compute the number of array elements
> > >
> > > But now it actually fails to build with the magic check.
> > >
> > > Change TRACEEVAL_ARRAY_SIZE() to handle NULL for both keys and vals, by
> > > not only having:
> > >
> > > #define TRACEEVAL_ARRAY_SIZE(data) \
> > > ((void *)(data) == NULL ? 0 : __TRACEEVAL_ARRAY_SIZE(data))
> > >
> > > But that is not enough, as gcc still evaluates the second part, and it
> > > will fail to build. To handle this, change that to:
> > >
> > > #define __TRACEEVAL_ARRAY_SIZE(data) \
> > > ((sizeof(data) / (sizeof((data)[0])) + 0) + \
> > >
> > > The above adds " + 0" to the "sizeof((data)[0])" which quiets the warning
> > > mentioned above (the addition of zero breaks the normal pattern of finding
> > > an array size).
> > >
> > > (int)(sizeof(struct { \
> > > int:(-!!(__builtin_types_compatible_p(typeof(data), \
> > > typeof(&((data)[0]))) && \
> > > (void *)(data) != NULL)); \
> > >
> > > Added "&& (void *)(data) != NULL" that makes the above return false (zero)
> > > for a static array and NULL, which is exactly what we want.
> >
> > Don't we already know it's not NULL because of the check in
> > TRACEEVAL_ARRAY_SIZE()? Or do we really need to check for NULL in both
> > macros?
>
> Unfortunately what happens is that the compiler still checks the above. So
> if we have just:
>
> (int)(sizeof(struct { \
> int:(-!!(__builtin_types_compatible_p(typeof(data), \
> typeof(&((data)[0])))));
>
>
> Then the with NULL turns into:
>
> struct { int: -1; }
>
> and fails the compile because:
>
> __builtin_types_compatible_p(typeof(NULL), typeof(&((NULL)[0])))
>
> Returns true.
>
> So if we pair that with (void *)(data) != NULL, it will then return false
> and turns into:
>
> struct { int: 0; }
>
> Which is valid.
Sounds good. If you haven't landed this already you can add:
Reviewed-by: Ross Zwisler <zwisler@google.com>
prev parent reply other threads:[~2023-10-17 14:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-06 1:22 [PATCH v2] libtraceeval: Have TRACEEVAL_ARRAY_SIZE() handle NULL pointer Steven Rostedt
2023-10-11 22:33 ` Ross Zwisler
2023-10-11 23:14 ` Steven Rostedt
2023-10-17 14:47 ` Ross Zwisler [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231017144756.GA3977037@google.com \
--to=zwisler@google.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=stevie.6strings@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).