On 12/17/2015 06:19 AM, Pádraig Brady wrote:
-     long int result = INT_MULTIPLY_WRAPV (a, b);
+     long int result = a * b;

This doesn't look right, because a * b could overflow which would lead to undefined behavior. INT_MULTIPLY_WRAPV does not rely on undefined behavior due to integer overflow, which is the point of that example.

I tracked this down to what appears to be a compiler bug in GCC, filed a bug report with the GCC folks (GCC bug 68971), and installed the attached patch instead.

>From efdcc1b2837adbf262e160747b23215712ffdafd Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Thu, 17 Dec 2015 12:56:55 -0800
Subject: [PATCH] intprops-test: work around GCC bug 68971
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Pádraig Brady in:
http://lists.gnu.org/archive/html/bug-gnulib/2015-12/msg00011.html
* tests/test-intprops.c: Ignore -Woverflow in GCC 6 and earlier.
(main): Add a case that better tests 64-bit long in this area.
---
 ChangeLog             |  8 ++++++++
 tests/test-intprops.c | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 6bc0a49..4fb4b97 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2015-12-17  Paul Eggert  <egg...@cs.ucla.edu>
+
+	intprops-test: work around GCC bug 68971
+	Problem reported by Pádraig Brady in:
+	http://lists.gnu.org/archive/html/bug-gnulib/2015-12/msg00011.html
+	* tests/test-intprops.c: Ignore -Woverflow in GCC 6 and earlier.
+	(main): Add a case that better tests 64-bit long in this area.
+
 2015-12-09  Pavel Raiskup  <prais...@redhat.com>
 
 	gnulib-tool: allow multiple --local-dir usage
diff --git a/tests/test-intprops.c b/tests/test-intprops.c
index 6b11417..82dc9bf 100644
--- a/tests/test-intprops.c
+++ b/tests/test-intprops.c
@@ -21,6 +21,14 @@
 #if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
 # pragma GCC diagnostic ignored "-Woverlength-strings"
 # pragma GCC diagnostic ignored "-Wtype-limits"
+
+/* Work around a bug in GCC 5.3.1 and earlier; see:
+   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68971
+   Hope it will be fixed by the time GCC 6 comes out.  */
+# if __GNUC__ < 6
+#  pragma GCC diagnostic ignored "-Woverflow"
+# endif
+
 #endif
 
 #include <config.h>
@@ -30,6 +38,7 @@
 
 #include <stdbool.h>
 #include <inttypes.h>
+#include <limits.h>
 
 #include "macros.h"
 
@@ -328,6 +337,18 @@ main (void)
             || result == INT_MIN * (long int) INT_MIN);
   }
 
+# ifdef LLONG_MAX
+  {
+    long long int result;
+    ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX, LONG_MAX, &result)
+            == (LLONG_MAX / LONG_MAX < LONG_MAX));
+    ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX, LONG_MAX, &result)
+            || result == LONG_MAX * (long long int) LONG_MAX);
+    ASSERT (INT_MULTIPLY_WRAPV (LONG_MIN, LONG_MIN, &result)
+            || result == LONG_MIN * (long long int) LONG_MIN);
+  }
+# endif
+
   #define CHECK_QUOTIENT(a, b, v) VERIFY (INT_DIVIDE_OVERFLOW (a, b) == (v))
 
   CHECK_QUOTIENT (INT_MIN, -1L,
-- 
2.5.0

Reply via email to