Re: [PATCH v2 2/4] selftests/nolibc: split out the structure member compatibility checks
From: David Laight
Date: Wed Dec 02 2026 - 06:29:53 EST
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?
I might have called them (struct_1, struct_2, member).
Those variables don't need protecting with a leading _ ...
> + bool different = false; \
... but, in theory, that one does.
(Clearly the calls are very limited.)
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;
>