PATCH: Use time_t in time/offtime.c

H.J. Lu hjl.tools@gmail.com
Thu Mar 15 23:37:00 GMT 2012


On Thu, Mar 15, 2012 at 2:10 PM, Paul Eggert <eggert@cs.ucla.edu> wrote:
> On 03/15/2012 01:17 PM, H.J. Lu wrote:
>>    days = *t / SECS_PER_DAY;
>> +  if (days != *t / SECS_PER_DAY)
>> +    goto overflow;
>
> This is not a reliable way to test for integer overflow.
> Since 'days' is signed, the compiler can assume that the
> assignment does not overflow, and can ignore the 'if' entirely.
>
> Instead, I suggest changing 'days' to be of type time_t,
> so that overflow cannot occur here.
>
>> @@ -65,6 +67,9 @@ __offtime (t, offset, tp)
>>        /* Guess a corrected year, assuming 365 days per year.  */
>>        long int yg = y + days / 365 - (days % 365 < 0);
>>
>> +      if (yg < 0)
>> +     goto overflow;
>> +
>
> Similarly here.

Here is the patch to use time_t on days, rem, y and yg in time/offtime.c.
It passed tests under Linux/x32.

Thanks.

-- 
H.J.
---
2012-03-15  H.J. Lu  <hongjiu.lu@intel.com>

	* time/offtime.c (__offtime): Use time_t on days, rem, y and yg.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libc-offtime.patch
Type: text/x-patch
Size: 812 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20120315/9bf330f5/attachment.bin>


More information about the Libc-alpha mailing list