[PATCH 1/3] selftests/nolibc: split out the structure member compatibility checks
From: Thomas Weißschuh
Date: Wed Nov 25 2026 - 04:44:27 EST
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 1157b521e65b..0eebd534bdb7 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) ({ \
+ bool different = false; \
+ \
+ 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;
--
2.56.1