[PATCH] Correct timespec implementation [BZ #26232]
Florian Weimer
fweimer@redhat.com
Tue Jul 14 12:18:18 GMT 2020
* H. J. Lu:
> There are
>
> time_t
> support_timespec_ns (struct timespec time)
> {
> time_t time_ns;
> if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns))
> return time.tv_sec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
> if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns))
> return time.tv_nsec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
> return time_ns;
> }
>
> Even if support_timespec_ns is changed to return long long, we still
> may need to keep
>
> time_t time_ns;
>
> for
>
> if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns))
>
> and
>
> if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns))
>
> It looks odd to return long long here.
Why?
You can use the GCC built-ins for checked arithmetic, they support mixed
types.
Thanks,
Florian
More information about the Libc-alpha
mailing list