mktime: avoid infinite loop
authorEric Blake <eblake@redhat.com>
Thu, 27 Jan 2011 17:06:08 +0000 (10:06 -0700)
committerEric Blake <eblake@redhat.com>
Sat, 29 Jan 2011 17:47:58 +0000 (10:47 -0700)
* m4/mktime.m4 (AC_FUNC_MKTIME): Avoid overflow on possibly-signed
type; behavior is still undefined but portable to all known targets.
Reported by Rich Felker.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
m4/mktime.m4

index 475d1bd..3a00f5a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-01-29  Eric Blake  <eblake@redhat.com>
+
+       mktime: avoid infinite loop
+       * m4/mktime.m4 (AC_FUNC_MKTIME): Avoid overflow on possibly-signed
+       type; behavior is still undefined but portable to all known targets.
+       Reported by Rich Felker.
+
 2011-01-29  Simon Josefsson  <simon@josefsson.org>
 
        rename, unlink, same-inode: Relicense.
index f2b131b..7836b76 100644 (file)
@@ -1,4 +1,4 @@
-# serial 17
+# serial 18
 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2011 Free Software Foundation,
 dnl Inc.
 dnl This file is free software; the Free Software Foundation
@@ -165,20 +165,22 @@ main ()
   int result = 0;
   time_t t, delta;
   int i, j;
+  int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
+  int time_t_signed = ! ((time_t) 0 < (time_t) -1);
 
   /* This test makes some buggy mktime implementations loop.
      Give up after 60 seconds; a mktime slower than that
      isn't worth using anyway.  */
   alarm (60);
 
-  for (;;)
-    {
-      t = (time_t_max << 1) + 1;
-      if (t <= time_t_max)
-        break;
-      time_t_max = t;
-    }
-  time_t_min = - ((time_t) ~ (time_t) 0 == (time_t) -1) - time_t_max;
+  time_t_max = (! time_t_signed
+                ? (time_t) -1
+                : ~ (~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1)));
+  time_t_min = (! time_t_signed
+                ? (time_t) 0
+                : time_t_signed_magnitude
+                ? ~ (time_t) 0
+                : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1));
 
   delta = time_t_max / 997; /* a suitable prime number */
   for (i = 0; i < N_STRINGS; i++)