Re: [PATCH v2 2/4] selftests/nolibc: split out the structure member compatibility checks
From: Willy Tarreau
Date: Sat Dec 05 2026 - 09:31:42 EST
On Wed, Dec 02, 2026 at 11:29:23AM +0000, David Laight wrote:
> On Wed, 02 Dec 2026 08:29:47 +0100
> Thomas Weißschuh <linux@xxxxxxxxxxxxxx> wrote:
>
> > A future test will add more similar tests.
> >
> > Factor out a helper to validate that two structures have compatible
> > members.
> >
> > Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
> > ---
> > tools/testing/selftests/nolibc/nolibc-test.c | 30 +++++++++++++++++-----------
> > 1 file changed, 18 insertions(+), 12 deletions(-)
> >
> > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> > index a924d53260fd..fce755ffed14 100644
> > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > @@ -58,6 +58,18 @@
> > #define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2)))
> > #define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1)
> >
> > +#define struct_members_identical(_member, _left, _right) ({ \
>
> Maybe put the member parameter last?
Honestly it's a matter or taste, habit or context you're working on. Most
likely depending on how you start you'll use either one or the other.
> I might have called them (struct_1, struct_2, member).
> Those variables don't need protecting with a leading _ ...
Sure but sometimes it makes it more obvious when reading the rest of the
macro what is an argument and what is not, like in the series of
different |= ... statements after that.
> > + bool different = false; \
>
> ... but, in theory, that one does.
> (Clearly the calls are very limited.)
Indeed it could, though unlikely to cause a problem, but might still
warrant a last-minute change without emitting a 3rd version.
Overall this looks good to me.
Reviewed-by: Willy Tarreau <w@xxxxxx>
Willy
> David
>
> > + \
> > + different |= sizeof((_left)._member) != sizeof((_right)._member); \
> > + different |= offsetof(__typeof__(_left), _member) != \
> > + offsetof(__typeof__(_right), _member); \
> > + different |= !__builtin_types_compatible_p(__typeof__((_left)._member), \
> > + __typeof__((_right)._member)); \
> > + \
> > + !different; \
> > +})
> > +
> > /* will be used to test initialization of environ */
> > static char **test_envp;
> >
> > @@ -1903,22 +1915,16 @@ int test_time_types(void)
> > struct timespec ts;
> >
> > if (!__builtin_types_compatible_p(time_t, __kernel_time64_t))
> > - return 1;
> > + return __LINE__;
> >
> > if (sizeof(ts) != sizeof(kts))
> > - return 1;
> > -
> > - if (!__builtin_types_compatible_p(__typeof__(ts.tv_sec), __typeof__(kts.tv_sec)))
> > - return 1;
> > -
> > - if (!__builtin_types_compatible_p(__typeof__(ts.tv_nsec), __typeof__(kts.tv_nsec)))
> > - return 1;
> > + return __LINE__;
> >
> > - if (offsetof(__typeof__(ts), tv_sec) != offsetof(__typeof__(kts), tv_sec))
> > - return 1;
> > + if (!struct_members_identical(tv_sec, ts, kts))
> > + return __LINE__;
> >
> > - if (offsetof(__typeof__(ts), tv_nsec) != offsetof(__typeof__(kts), tv_nsec))
> > - return 1;
> > + if (!struct_members_identical(tv_nsec, ts, kts))
> > + return __LINE__;
> > #endif /* NOLIBC */
> >
> > return 0;
> >
>