bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

avoid integer overflow in mktime.m4


From: Ralf Wildenhues
Subject: avoid integer overflow in mktime.m4
Date: Tue, 19 Dec 2006 00:08:47 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

$ gcc-4.3 --version
| gcc (GCC) 4.3.0 20061215 (experimental)
$ gnulib-tool --with-tests --test
[ interrupt at a convenient location ]
$ .../configure CC=gcc-test
[...]
| checking for working mktime...

This test hangs, because the newer GCC exploits at -O2 the fact that
integer overflow produces undefined behavior: the loop stopping
criterion is never fulfilled:
|     for (j = 1; 0 < j; j *= 2)

The patch below fixes that.  OK to apply to gnulib/Autoconf?

FYI, here's a short program to expose the issue: it never prints
"isnull" with 'gcc-4.3 -O2'.  This isn't the case with 4.1.2, I
haven't tested any other intermediate versions.

Cheers,
Ralf

#include <stdio.h>
int main()
{
  int i = 1, j;
  for (j=1; j<128; j++) {
    i = i + i;
    printf("%d %s\n", j, i == 0 ? "isnull" : "nonnull");
  }
  return 0;
}

2006-12-19  Ralf Wildenhues  <address@hidden>

        Sync from Autoconf:
        * m4/mktime.m4 (AC_FUNC_MKTIME): Avoid undefined behavior due to
        integer overflow in runtime test, exposed with unreleased GCC
        4.3.0.

Index: m4/mktime.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/mktime.m4,v
retrieving revision 1.25
diff -u -r1.25 mktime.m4
--- m4/mktime.m4        8 Sep 2006 22:48:25 -0000       1.25
+++ m4/mktime.m4        18 Dec 2006 23:08:35 -0000
@@ -171,7 +171,8 @@
 main ()
 {
   time_t t, delta;
-  int i, j;
+  int i;
+  unsigned int j;
 
   /* This test makes some buggy mktime implementations loop.
      Give up after 60 seconds; a mktime slower than that


2006-12-19  Ralf Wildenhues  <address@hidden>

        * lib/autoconf/functions.m4 (AC_FUNC_MKTIME): Avoid undefined
        behavior due to integer overflow in runtime test, exposed with
        unreleased GCC 4.3.0.

Index: lib/autoconf/functions.m4
===================================================================
RCS file: /cvsroot/autoconf/autoconf/lib/autoconf/functions.m4,v
retrieving revision 1.118
diff -u -r1.118 functions.m4
--- lib/autoconf/functions.m4   16 Dec 2006 05:38:41 -0000      1.118
+++ lib/autoconf/functions.m4   18 Dec 2006 23:08:08 -0000
@@ -1125,7 +1125,8 @@
 main ()
 {
   time_t t, delta;
-  int i, j;
+  int i;
+  unsigned int j;
 
   /* This test makes some buggy mktime implementations loop.
      Give up after 60 seconds; a mktime slower than that




reply via email to

[Prev in Thread] Current Thread [Next in Thread]