Mercurial > hg > octave-lyh > gnulib-hg
changeset 14245:21a50d7b2537
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.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Thu, 27 Jan 2011 10:06:08 -0700 |
parents | 8e21e37986ee |
children | f1e2d2684f0b |
files | ChangeLog m4/mktime.m4 |
diffstat | 2 files changed, 18 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- 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.
--- a/m4/mktime.m4 +++ b/m4/mktime.m4 @@ -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 @@ 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++)