[PATCH] tests: Fix LL->time_t->tv_sec conversion path

christian.ehrhardt at canonical.com christian.ehrhardt at canonical.com
Thu Apr 18 13:42:45 UTC 2024


From: Christian Ehrhardt <christian.ehrhardt at canonical.com>

Due to 64 bit time_t on armhf the initialization of some testcases that
use the pattern of `...tv-sec = (time_t) = 0x1234LL` now fail.

Example:
  tests/xselect.c:216:24: error: overflow in conversion from
  ‘long long int’ to ‘kernel_long_t’ {aka ‘long int’}
  changes value from ‘-3819351491602432273’
  to ‘-559038737’ [-Werror=overflow]
  216 |         tv_in.tv_sec = (time_t) 0xcafef00ddeadbeefLL;

Other places that used to cast from LL to time_t to assign to tv_sec
have been changed in 2023 by 7178658e "tests: avoid libc definition
for time_t in direct syscalls"
  -       ts->tv_sec = (time_t) 0xcafef00ddeadbeefLL;
  +       ts->tv_sec = (typeof(ts->tv_sec)) 0xcafef00ddeadbeefLL;

That avoids the issue where time64 on 32bit armhf leads to a mismatch.
kernel_old_timespec.h determines as SIZEOF_KERNEL_LONG_T == 4 and
thereby selecting `int` as type for tv_sec, but then time_t changed
to 64 bit.
Yet on the other hand tv_sec didn't change as it is a kernel type,
so the assignment triggers an error.
  old: 64bit -> explicit cast 32bit -> assign 32bit tv_sec
  new: 64bit -> explicit cast 64bit (time_t) -> assign 32bit tv_sec

The same pattern was yet unfixed in a few tests and hereby
resolved the same way as in 7178658e.

Signed-off-by: Christian Ehrhardt <christian.ehrhardt at canonical.com>
---
 tests/mq_sendrecv.c      | 4 ++--
 tests/recvmmsg-timeout.c | 2 +-
 tests/xselect.c          | 2 +-
 tests/xutimes.c          | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/tests/mq_sendrecv.c b/tests/mq_sendrecv.c
index 285e090a9..d1bde766a 100644
--- a/tests/mq_sendrecv.c
+++ b/tests/mq_sendrecv.c
@@ -195,11 +195,11 @@ main(void)
 	static const kernel_ulong_t bogus_prio =
 		(kernel_ulong_t) 0xdec0ded1defaced3ULL;
 	static const kernel_old_timespec_t bogus_tmout_data = {
-		.tv_sec = (time_t) 0xdeadfacebeeff00dLL,
+		.tv_sec = (typeof((kernel_old_timespec_t){0}.tv_sec)) 0xdeadfacebeeff00dLL,
 		.tv_nsec = (long) 0xfacefee1deadfeedLL,
 	};
 	static const kernel_old_timespec_t future_tmout_data = {
-		.tv_sec = (time_t) 0x7ea1fade7e57faceLL,
+		.tv_sec = (typeof((kernel_old_timespec_t){0}.tv_sec)) 0x7ea1fade7e57faceLL,
 		.tv_nsec = 999999999,
 	};
 	struct_sigevent bogus_sev_data = {
diff --git a/tests/recvmmsg-timeout.c b/tests/recvmmsg-timeout.c
index 62d791ec0..4eb01d332 100644
--- a/tests/recvmmsg-timeout.c
+++ b/tests/recvmmsg-timeout.c
@@ -57,7 +57,7 @@ main(void)
 	       fds[0], &mh, (long long) ts->tv_sec,
 	       zero_extend_signed_to_ull(ts->tv_nsec), sprintrc(rc));
 
-	ts->tv_sec = (time_t) 0xcafef00ddeadbeefLL;
+	ts->tv_sec = (typeof(ts->tv_sec)) 0xcafef00ddeadbeefLL;
 	ts->tv_nsec = (long) 0xbadc0dedfacefeedLL;
 
 	rc = recv_mmsg(fds[0], &mh, 1, 0, ts);
diff --git a/tests/xselect.c b/tests/xselect.c
index 60522d728..1b8d934ff 100644
--- a/tests/xselect.c
+++ b/tests/xselect.c
@@ -213,7 +213,7 @@ main(void)
 	 * Very odd timeout.
 	 */
 	*l_rs = (1UL << fds[0]) | (1UL << fds[1]);
-	tv_in.tv_sec = (time_t) 0xcafef00ddeadbeefLL;
+	tv_in.tv_sec = (typeof(tv_in.tv_sec)) 0xcafef00ddeadbeefLL;
 	tv_in.tv_usec = (suseconds_t) 0xbadc0dedfacefeedLL;
 	memcpy(tv, &tv_in, sizeof(tv_in));
 	rc = xselect(nfds, a_rs, a_rs, a_rs, a_tv);
diff --git a/tests/xutimes.c b/tests/xutimes.c
index 8c7b2b675..2c5dbefb7 100644
--- a/tests/xutimes.c
+++ b/tests/xutimes.c
@@ -86,7 +86,7 @@ main(void)
 
 	tv[0].tv_sec = 0xdeadbeefU;
 	tv[0].tv_usec = 0xfacefeedU;
-	tv[1].tv_sec = (time_t) 0xcafef00ddeadbeefLL;
+	tv[1].tv_sec = (typeof(tv[1].tv_sec)) 0xcafef00ddeadbeefLL;
 	tv[1].tv_usec = (suseconds_t) 0xbadc0dedfacefeedLL;
 
 	k_utimes(kfname, (uintptr_t) tv);
-- 
2.34.1



More information about the Strace-devel mailing list