]> Savannah Git Hosting - gnulib.git/commitdiff
calloc-gnu tests: Avoid a test failure with clang.
authorBruno Haible <bruno@clisp.org>
Sat, 6 Jun 2020 08:14:24 +0000 (10:14 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 6 Jun 2020 08:16:09 +0000 (10:16 +0200)
* tests/test-calloc-gnu.c (main): Mark the pointer variable as
'volatile', to defeat compiler optimizations.

ChangeLog
tests/test-calloc-gnu.c

index 7e3e708521a4ed9e6854c416819b52be3d17ac42..747d7e5364bcccc52cd857c4bd5cc70fbf71771e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-06-06  Bruno Haible  <bruno@clisp.org>
+
+       calloc-gnu tests: Avoid a test failure with clang.
+       * tests/test-calloc-gnu.c (main): Mark the pointer variable as
+       'volatile', to defeat compiler optimizations.
+
 2020-06-01  Paul Eggert  <eggert@cs.ucla.edu>
 
        getloadavg: fix double-increment bug
index c13ba475fa20ae7aaa7da24e8e435c72ffeea10f..2ae392565c918bd726796d4e58bdd91a60836fe0 100644 (file)
@@ -36,20 +36,26 @@ int
 main ()
 {
   /* Check that calloc (0, 0) is not a NULL pointer.  */
-  void *p = calloc (0, 0);
-  if (p == NULL)
-    return 1;
-  free (p);
+  {
+    void * volatile p = calloc (0, 0);
+    if (p == NULL)
+      return 1;
+    free (p);
+  }
 
   /* Check that calloc fails when requested to allocate a block of memory
      larger than SIZE_MAX bytes.
-     We use eight (), not 8, to avoid a compiler warning from GCC 7.  */
-  p = calloc ((size_t) -1 / 8 + 1, eight ());
-  if (p != NULL)
-    {
-      free (p);
-      return 1;
-    }
+     We use eight (), not 8, to avoid a compiler warning from GCC 7.
+     'volatile' is needed to defeat an incorrect optimization by clang 10,
+     see <https://bugs.llvm.org/show_bug.cgi?id=46055>.  */
+  {
+    void * volatile p = calloc ((size_t) -1 / 8 + 1, eight ());
+    if (p != NULL)
+      {
+        free (p);
+        return 2;
+      }
+  }
 
   return 0;
 }