f3ebb0520b8d0a12fbd2dd61bdf1722e10033d8a
[openwrt.git] / tools / findutils / patches / 21-Fix-time_t-vs-long-int-mismatches.patch
1 >From 0078a6c784da339cc529b4f0bf1156ca52692e4c Mon Sep 17 00:00:00 2001
2 From: Adam Borowski <kilobyte@angband.pl>
3 Date: Thu, 6 Jun 2013 18:41:53 +0000
4 Subject: [PATCH] Fix time_t vs long int mismatches.
5
6 Old gnulibs used randomly either time_t or long int, with a compile-time
7 assert to ensure sizeof(time_t) <= sizeof(long int).  This is not the
8 case on x32 where the machine word is 32 bit, yet time_t is 64 bit to
9 be able to handle dates after 2038.
10
11 This is not relevant for modern versions of gnulib which has rewritten
12 this code, but, sadly, findutils 4.4.* uses an embedded copy of ancient
13 gnulib.
14 ---
15  gnulib/lib/getdate.y | 46 ++++++++++++++++++++++++----------------------
16  1 file changed, 24 insertions(+), 22 deletions(-)
17
18 diff --git a/gnulib/lib/getdate.y b/gnulib/lib/getdate.y
19 index e292f5e..347cc77 100644
20 --- a/gnulib/lib/getdate.y
21 +++ b/gnulib/lib/getdate.y
22 @@ -112,16 +112,18 @@
23  /* Lots of this code assumes time_t and time_t-like values fit into
24     long int.  It also assumes that signed integer overflow silently
25     wraps around, but there's no portable way to check for that at
26 -   compile-time.  */
27 +   compile-time.
28 +   [1KB]: replaced suspicious uses of long_t by time_t.
29  verify (TYPE_IS_INTEGER (time_t));
30  verify (LONG_MIN <= TYPE_MINIMUM (time_t) && TYPE_MAXIMUM (time_t) <= LONG_MAX);
31 +*/
32  
33  /* An integer value, and the number of digits in its textual
34     representation.  */
35  typedef struct
36  {
37    bool negative;
38 -  long int value;
39 +  time_t value;
40    size_t digits;
41  } textint;
42  
43 @@ -206,7 +208,7 @@ typedef struct
44  union YYSTYPE;
45  static int yylex (union YYSTYPE *, parser_control *);
46  static int yyerror (parser_control const *, char const *);
47 -static long int time_zone_hhmm (textint, long int);
48 +static time_t time_zone_hhmm (textint, time_t);
49  
50  /* Extract into *PC any date and time info from a string of digits
51     of the form e.g., YYYYMMDD, YYMMDD, HHMM, HH (and sometimes YYY,
52 @@ -817,8 +819,8 @@ static table const military_table[] =
53     minutes.  If MM is negative, then S is of the form HHMM and needs
54     to be picked apart; otherwise, S is of the form HH.  */
55  
56 -static long int
57 -time_zone_hhmm (textint s, long int mm)
58 +static time_t
59 +time_zone_hhmm (textint s, time_t mm)
60  {
61    if (mm < 0)
62      return (s.value / 100) * 60 + s.value % 100;
63 @@ -884,7 +886,7 @@ lookup_zone (parser_control const *pc, char const *name)
64     measured in seconds, ignoring leap seconds.
65     The body of this function is taken directly from the GNU C Library;
66     see src/strftime.c.  */
67 -static long int
68 +static time_t
69  tm_diff (struct tm const *a, struct tm const *b)
70  {
71    /* Compute intervening leap days correctly even if year is negative.
72 @@ -896,9 +898,9 @@ tm_diff (struct tm const *a, struct tm const *b)
73    int a400 = SHR (a100, 2);
74    int b400 = SHR (b100, 2);
75    int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
76 -  long int ayear = a->tm_year;
77 -  long int years = ayear - b->tm_year;
78 -  long int days = (365 * years + intervening_leap_days
79 +  time_t ayear = a->tm_year;
80 +  time_t years = ayear - b->tm_year;
81 +  time_t int days = (365 * years + intervening_leap_days
82                    + (a->tm_yday - b->tm_yday));
83    return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
84                 + (a->tm_min - b->tm_min))
85 @@ -1200,7 +1202,7 @@ bool
86  get_date (struct timespec *result, char const *p, struct timespec const *now)
87  {
88    time_t Start;
89 -  long int Start_ns;
90 +  time_t Start_ns;
91    struct tm const *tmp;
92    struct tm tm;
93    struct tm tm0;
94 @@ -1407,16 +1409,16 @@ get_date (struct timespec *result, char const *p, struct timespec const *now)
95                  problem, set the time zone to 1 hour behind UTC temporarily
96                  by setting TZ="XXX1:00" and try mktime again.  */
97  
98 -             long int time_zone = pc.time_zone;
99 -             long int abs_time_zone = time_zone < 0 ? - time_zone : time_zone;
100 -             long int abs_time_zone_hour = abs_time_zone / 60;
101 +             time_t time_zone = pc.time_zone;
102 +             time_t abs_time_zone = time_zone < 0 ? - time_zone : time_zone;
103 +             time_t abs_time_zone_hour = abs_time_zone / 60;
104               int abs_time_zone_min = abs_time_zone % 60;
105               char tz1buf[sizeof "XXX+0:00"
106                           + sizeof pc.time_zone * CHAR_BIT / 3];
107               if (!tz_was_altered)
108                 tz0 = get_tz (tz0buf);
109               sprintf (tz1buf, "XXX%s%ld:%02d", "-" + (time_zone < 0),
110 -                      abs_time_zone_hour, abs_time_zone_min);
111 +                      (long int)abs_time_zone_hour, abs_time_zone_min);
112               if (setenv ("TZ", tz1buf, 1) != 0)
113                 goto fail;
114               tz_was_altered = true;
115 @@ -1439,7 +1441,7 @@ get_date (struct timespec *result, char const *p, struct timespec const *now)
116  
117        if (pc.zones_seen)
118         {
119 -         long int delta = pc.time_zone * 60;
120 +         time_t delta = pc.time_zone * 60;
121           time_t t1;
122  #ifdef HAVE_TM_GMTOFF
123           delta -= tm.tm_gmtoff;
124 @@ -1486,16 +1488,16 @@ get_date (struct timespec *result, char const *p, struct timespec const *now)
125          must be applied before relative times, and if mktime is applied
126          again the time zone will be lost.  */
127        {
128 -       long int sum_ns = pc.seconds.tv_nsec + pc.rel.ns;
129 -       long int normalized_ns = (sum_ns % BILLION + BILLION) % BILLION;
130 +       time_t sum_ns = pc.seconds.tv_nsec + pc.rel.ns;
131 +       time_t normalized_ns = (sum_ns % BILLION + BILLION) % BILLION;
132         time_t t0 = Start;
133 -       long int d1 = 60 * 60 * pc.rel.hour;
134 +       time_t d1 = 60 * 60 * pc.rel.hour;
135         time_t t1 = t0 + d1;
136 -       long int d2 = 60 * pc.rel.minutes;
137 +       time_t d2 = 60 * pc.rel.minutes;
138         time_t t2 = t1 + d2;
139 -       long int d3 = pc.rel.seconds;
140 +       time_t d3 = pc.rel.seconds;
141         time_t t3 = t2 + d3;
142 -       long int d4 = (sum_ns - normalized_ns) / BILLION;
143 +       time_t d4 = (sum_ns - normalized_ns) / BILLION;
144         time_t t4 = t3 + d4;
145  
146         if ((d1 / (60 * 60) ^ pc.rel.hour)
147 @@ -1542,7 +1544,7 @@ main (int ac, char **av)
148         printf ("Bad format - couldn't convert.\n");
149        else if (! (tm = localtime (&d.tv_sec)))
150         {
151 -         long int sec = d.tv_sec;
152 +         time_t sec = d.tv_sec;
153           printf ("localtime (%ld) failed\n", sec);
154         }
155        else
156 -- 
157 1.8.3.rc3
158