This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH: Use time_t in time/offtime.c


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.
2012-03-15  H.J. Lu  <hongjiu.lu@intel.com>
 
	* time/offtime.c (__offtime): Use time_t on days, rem, y and yg.

diff --git a/time/offtime.c b/time/offtime.c
index 1ccd6a8..56c49f0 100644
--- a/time/offtime.c
+++ b/time/offtime.c
@@ -31,7 +31,7 @@ __offtime (t, offset, tp)
      long int offset;
      struct tm *tp;
 {
-  long int days, rem, y;
+  time_t days, rem, y;
   const unsigned short int *ip;
 
   days = *t / SECS_PER_DAY;
@@ -63,7 +63,7 @@ __offtime (t, offset, tp)
   while (days < 0 || days >= (__isleap (y) ? 366 : 365))
     {
       /* Guess a corrected year, assuming 365 days per year.  */
-      long int yg = y + days / 365 - (days % 365 < 0);
+      time_t yg = y + days / 365 - (days % 365 < 0);
 
       /* Adjust DAYS and Y to match the guessed year.  */
       days -= ((yg - y) * 365

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]