libtraceeval: Have TRACEEVAL_ARRAY_SIZE() handle NULL pointer
PATCH v2 1/1 libtraceeval: Have TRACEEVAL_ARRAY_SIZE() handle NULL pointer
From: "Steven Rostedt (Google)" <[email protected]>
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.
})))
---
Changes since v1: https://lore.kernel.org/all/[email protected]/
- Fixed error in placement of parenthesis.
include/traceeval.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
include/traceeval.h +9 -5
@@ -29,13 +29,17 @@* * struct traceeval_data keys[] = { ... }; */ -#define TRACEEVAL_ARRAY_SIZE (data) \ - ((sizeof(data) / sizeof((data)[0])) + \ +#define __TRACEEVAL_ARRAY_SIZE (data) \ + ((sizeof(data) / (sizeof((data)[0]) + 0)) + \ (int)(sizeof(struct { \ int:(-!!(__builtin_types_compatible_p(typeof(data), \ - typeof(&((data)[0]))))); \ + typeof(&((data)[0]))) && \ + (void *)(data) != NULL)); \ }))) +#define TRACEEVAL_ARRAY_SIZE (data) \ + ((void *)(data) == NULL ? 0 : __TRACEEVAL_ARRAY_SIZE(data)) +/* Data type distinguishers */ enum traceeval_data_type {TRACEEVAL_TYPE_NONE ,
@@ -191,7 +195,7 @@ struct traceeval;#define traceeval_init (keys, vals) \ traceeval_init_size(keys, vals, \ TRACEEVAL_ARRAY_SIZE(keys), \ - (void *)vals == NULL ? 0 : TRACEEVAL_ARRAY_SIZE(vals)) + TRACEEVAL_ARRAY_SIZE(vals))#define traceeval_init_size (keys, vals, nr_keys, nr_vals) \ traceeval_init_data_size(keys, vals, nr_keys, nr_vals, \
@@ -211,7 +215,7 @@ int traceeval_insert_size(struct traceeval *teval,#define traceeval_insert (teval, keys, vals) \ traceeval_insert_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), \ - vals, (void *)vals == NULL ? 0 : TRACEEVAL_ARRAY_SIZE(vals)) + vals, TRACEEVAL_ARRAY_SIZE(vals))int traceeval_remove_size (struct traceeval *teval,const struct traceeval_data *keys,size_t nr_keys);
Signature · 1 line
2.40.1
Quoted 33 lines · Steven Rostedt
From: "Steven Rostedt (Google)" <[email protected]>
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).
Quoted 7 lines
(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?
Quoted 2 lines
})))
A few random parens in the changelog. :)
Quoted 56 lines
Signed-off-by Steven Rostedt (Google) <[email protected]>---
Changes since v1: https://lore.kernel.org/all/[email protected]/- Fixed error in placement of parenthesis.
include/traceeval.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)include/traceeval.h +9
-5@@ -29,13 +29,17 @@* * struct traceeval_data keys[] = { ... }; */ -#define TRACEEVAL_ARRAY_SIZE (data) \ - ((sizeof(data) / sizeof((data)[0])) + \ +#define __TRACEEVAL_ARRAY_SIZE (data) \ + ((sizeof(data) / (sizeof((data)[0]) + 0)) + \ (int)(sizeof(struct { \ int:(-!!(__builtin_types_compatible_p(typeof(data), \ - typeof(&((data)[0]))))); \ + typeof(&((data)[0]))) && \ + (void *)(data) != NULL)); \ }))) +#define TRACEEVAL_ARRAY_SIZE (data) \ + ((void *)(data) == NULL ? 0 : __TRACEEVAL_ARRAY_SIZE(data)) +/* Data type distinguishers */ enum traceeval_data_type {TRACEEVAL_TYPE_NONE ,@@ -191,7 +195,7 @@ struct traceeval;#define traceeval_init (keys, vals) \ traceeval_init_size(keys, vals, \ TRACEEVAL_ARRAY_SIZE(keys), \ - (void *)vals == NULL ? 0 : TRACEEVAL_ARRAY_SIZE(vals)) + TRACEEVAL_ARRAY_SIZE(vals))#define traceeval_init_size (keys, vals, nr_keys, nr_vals) \ traceeval_init_data_size(keys, vals, nr_keys, nr_vals, \@@ -211,7 +215,7 @@ int traceeval_insert_size(struct traceeval *teval,#define traceeval_insert (teval, keys, vals) \ traceeval_insert_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), \ - vals, (void *)vals == NULL ? 0 : TRACEEVAL_ARRAY_SIZE(vals)) + vals, TRACEEVAL_ARRAY_SIZE(vals))int traceeval_remove_size (struct traceeval *teval,const struct traceeval_data *keys,size_t nr_keys);Signature · 1 line
2.40.1
Quoted 39 lines · On Wed, 11 Oct 2023 16:33:48 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 38 lines · Steven Rostedt
From: "Steven Rostedt (Google)" <[email protected]>
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)); \
Quoted 6 lines
Quoted 2 lines
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.
Quoted 6 lines
Quoted 3 lines
})))A few random parens in the changelog. :)
Yeah, the code had it too and I had to fix it. But didn't update the change log. :-p
-- Steve
Ross Zwisler
Reviewed-byQuoted 65 lines · Steven Rostedt
Quoted 39 lines · On Wed, 11 Oct 2023 16:33:48 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 38 lines · Steven Rostedt
From: "Steven Rostedt (Google)" <[email protected]>
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)); \Quoted 6 lines
Quoted 2 lines
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:
include/traceeval.h +0
-0(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])))
Quoted 8 lines
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: