21-Fix-time_t-vs-long-int-mismatches.patch 5.8 KB

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