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

Dmitry V. Levin ldv at strace.io
Thu Apr 18 23:42:45 UTC 2024


On Thu, Apr 18, 2024 at 03:42:45PM +0200, christian.ehrhardt at canonical.com wrote:
> 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,

I think (typeof(bogus_tmout_data.tv_sec)) is slightly more readable.

>  		.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,

Likewise, (typeof(future_tmout_data.tv_sec)) seems to be more readable.

Applied with these two minor tweaks, thanks!


-- 
ldv


More information about the Strace-devel mailing list