This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
PATCH [4/n]: Support 64bit time_t and 32bit long
- From: "H.J. Lu" <hongjiu dot lu at intel dot com>
- To: GNU C Library <libc-alpha at sourceware dot org>
- Date: Thu, 15 Mar 2012 12:38:43 -0700
- Subject: PATCH [4/n]: Support 64bit time_t and 32bit long
- Reply-to: "H.J. Lu" <hjl dot tools at gmail dot com>
Linux/x32 uses 64bit time_t and 32bit long. This patch updates
ydhms_diff to support TIME_T_MAX != LONG_MAX. It also updates __offtime
to propeyly check overflow when converting time_t to long.
H.J.
----
2012-03-15 H.J. Lu <hongjiu.lu@intel.com>
* time/mktime.c (ydhms_diff): Check TIME_T_MAX instead of
LONG_MAX.
* time/offtime.c (__offtime): Add more overflow check.
diff --git a/time/mktime.c b/time/mktime.c
index 683d468..e96cc48 100644
--- a/time/mktime.c
+++ b/time/mktime.c
@@ -168,7 +168,7 @@ ydhms_diff (long int year1, long int yday1, int hour1, int min1, int sec1,
{
verify (C99_integer_division, -1 / 2 == 0);
verify (long_int_year_and_yday_are_wide_enough,
- INT_MAX <= LONG_MAX / 2 || TIME_T_MAX <= UINT_MAX);
+ INT_MAX <= TIME_T_MAX / 2 || TIME_T_MAX <= UINT_MAX);
/* Compute intervening leap days correctly even if year is negative.
Take care to avoid integer overflow here. */
diff --git a/time/offtime.c b/time/offtime.c
index 1ccd6a8..3746b99 100644
--- a/time/offtime.c
+++ b/time/offtime.c
@@ -35,6 +35,8 @@ __offtime (t, offset, tp)
const unsigned short int *ip;
days = *t / SECS_PER_DAY;
+ if (days != *t / SECS_PER_DAY)
+ goto overflow;
rem = *t % SECS_PER_DAY;
rem += offset;
while (rem < 0)
@@ -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;
+
/* Adjust DAYS and Y to match the guessed year. */
days -= ((yg - y) * 365
+ LEAPS_THRU_END_OF (yg - 1)
@@ -74,6 +79,7 @@ __offtime (t, offset, tp)
tp->tm_year = y - 1900;
if (tp->tm_year != y - 1900)
{
+overflow:
/* The year cannot be represented due to overflow. */
__set_errno (EOVERFLOW);
return 0;
--
1.7.6.5