]> Savannah Git Hosting - gnulib.git/commitdiff
parse-datetime: allow calculations to yield -1
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 5 Feb 2022 19:05:44 +0000 (11:05 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 5 Feb 2022 19:06:29 +0000 (11:06 -0800)
Problem reported by Jeremy Cantrell <https://bugs.gnu.org/50115>.
* lib/parse-datetime.y (parse_datetime_body): When calling mktime,
use an unmodifed and negative tm_wday or tm_yday to detect an error,
as a (time_t) -1 return value is valid on most hosts.
* tests/test-parse-datetime.c (main): Add a test for the bug.

ChangeLog
lib/parse-datetime.y
tests/test-parse-datetime.c

index 5445802ea2b88ec04d7284d56b85fd0ce60eecd4..18dcb3fe3f525afd9f238c79bc254f76f3f95586 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2022-02-05  Paul Eggert  <eggert@cs.ucla.edu>
+
+       parse-datetime: allow calculations to yield -1
+       Problem reported by Jeremy Cantrell <https://bugs.gnu.org/50115>.
+       * lib/parse-datetime.y (parse_datetime_body): When calling mktime,
+       use an unmodifed and negative tm_wday or tm_yday to detect an error,
+       as a (time_t) -1 return value is valid on most hosts.
+       * tests/test-parse-datetime.c (main): Add a test for the bug.
+
 2022-02-04  Paul Eggert  <eggert@cs.ucla.edu>
 
        userspec: help fix GNU ‘id’ incompatibility
index c40fdcef7f6c56518651c5af079c594923e6cdbc..9fc14c9d467524e20d2ef246447f9dbb007d55fd 100644 (file)
@@ -2076,21 +2076,20 @@ parse_datetime_body (struct timespec *result, char const *p,
       if (pc.days_seen && ! pc.dates_seen)
         {
           intmax_t dayincr;
-          if (INT_MULTIPLY_WRAPV ((pc.day_ordinal
-                                   - (0 < pc.day_ordinal
-                                      && tm.tm_wday != pc.day_number)),
-                                  7, &dayincr)
-              || INT_ADD_WRAPV ((pc.day_number - tm.tm_wday + 7) % 7,
-                                dayincr, &dayincr)
-              || INT_ADD_WRAPV (dayincr, tm.tm_mday, &tm.tm_mday))
-            Start = -1;
-          else
+          tm.tm_yday = -1;
+          if (! (INT_MULTIPLY_WRAPV ((pc.day_ordinal
+                                      - (0 < pc.day_ordinal
+                                         && tm.tm_wday != pc.day_number)),
+                                     7, &dayincr)
+                 || INT_ADD_WRAPV ((pc.day_number - tm.tm_wday + 7) % 7,
+                                   dayincr, &dayincr)
+                 || INT_ADD_WRAPV (dayincr, tm.tm_mday, &tm.tm_mday)))
             {
               tm.tm_isdst = -1;
               Start = mktime_z (tz, &tm);
             }
 
-          if (Start == (time_t) -1)
+          if (tm.tm_yday < 0)
             {
               if (debugging (&pc))
                 dbg_printf (_("error: day '%s' "
@@ -2156,8 +2155,9 @@ parse_datetime_body (struct timespec *result, char const *p,
           tm.tm_min = tm0.tm_min;
           tm.tm_sec = tm0.tm_sec;
           tm.tm_isdst = tm0.tm_isdst;
+          tm.tm_wday = -1;
           Start = mktime_z (tz, &tm);
-          if (Start == (time_t) -1)
+          if (tm.tm_wday < 0)
             {
               if (debugging (&pc))
                 dbg_printf (_("error: adding relative date resulted "
index 059c810cd1459f8079cea5c02c5e6902493daea3..1e7955bc96a0ab9745982b191cc2d27163011594 100644 (file)
@@ -398,6 +398,14 @@ main (_GL_UNUSED int argc, char **argv)
       ASSERT (result.tv_sec == thur2 + ((i + 3) % 7 - 7) * 24 * 3600);
     }
 
+  p = "1970-12-31T23:59:59+00:00 - 1 year";  /* Bug#50115 */
+  now.tv_sec = -1;
+  now.tv_nsec = 0;
+  ASSERT (parse_datetime (&result, p, &now));
+  LOG (p, now, result);
+  ASSERT (result.tv_sec == now.tv_sec
+          && result.tv_nsec == now.tv_nsec);
+
   p = "THURSDAY UTC+00";  /* The epoch was on Thursday.  */
   now.tv_sec = 0;
   now.tv_nsec = 0;