[PATCH] Correct timespec implementation [BZ #26232]

H.J. Lu hjl.tools@gmail.com
Tue Jul 14 12:04:23 GMT 2020


On Tue, Jul 14, 2020 at 4:42 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Tue, Jul 14, 2020 at 4:16 AM Florian Weimer <fweimer@redhat.com> wrote:
> >
> > * H. J. Lu via Libc-alpha:
> >
> > > -/* Convert TIME to nanoseconds stored in a long.
> > > -   Returns long maximum or minimum if the conversion overflows
> > > +/* Convert TIME to nanoseconds stored in a time_t.
> > > +   Returns time_t maximum or minimum if the conversion overflows
> > >     or underflows, respectively.  */
> > > -long
> > > +time_t
> > >  support_timespec_ns (struct timespec time)
> > >  {
> >
> > Why not use long long int as the type?
> >
>
> Using time_t has the least impact since most of the targets have time_t == long.
> I am checking in this patch and will post a followup patch with long long.
>

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.

-- 
H.J.


More information about the Libc-alpha mailing list