On AIX 7.3, with CC="ibm-clang", I see different configure test results
depending on optimization level:

With -O0:
checking whether realloc (0, 0) returns nonnull... no

With -O2:
checking whether realloc (0, 0) returns nonnull... yes

This can be reproduced standalone:

$ cat > foo.c
#include <stdlib.h>
int
main (void)
{
  void *p = realloc (0, 0); 
  int result = !p;
  free (p);
  return result;
}
$ ibm-clang foo.c && { ./a.out ; echo $?; }
1
$ ibm-clang -O2 foo.c && { ./a.out ; echo $?; }
0

So, like we did in
<https://lists.gnu.org/archive/html/bug-gnulib/2023-08/msg00149.html>,
we need to use 'volatile' to override the compiler's "knowledge".

The corresponding 'malloc' configure test does not have the same
problem, but it's safe to prevent the same "optimization" for
other compilers too.


2023-11-17  Bruno Haible  <br...@clisp.org>

        realloc-gnu: Fix wrong configure test result with optimizing ibm-clang.
        * m4/realloc.m4 (_AC_FUNC_REALLOC_IF): Use 'volatile', to defeat an
        ibm-clang optimization.
        * m4/malloc.m4 (_AC_FUNC_MALLOC_IF): Likewise.

diff --git a/m4/malloc.m4 b/m4/malloc.m4
index 096c262b17..8b8c56eef5 100644
--- a/m4/malloc.m4
+++ b/m4/malloc.m4
@@ -1,4 +1,4 @@
-# malloc.m4 serial 30
+# malloc.m4 serial 31
 dnl Copyright (C) 2007, 2009-2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -16,7 +16,8 @@ AC_DEFUN([_AC_FUNC_MALLOC_IF]
           [[#include <stdlib.h>
           ]],
           [[void *p = malloc (0);
-            int result = !p;
+            void * volatile vp = p;
+            int result = !vp;
             free (p);
             return result;]])
        ],
diff --git a/m4/realloc.m4 b/m4/realloc.m4
index fcd6253557..9911866f58 100644
--- a/m4/realloc.m4
+++ b/m4/realloc.m4
@@ -1,4 +1,4 @@
-# realloc.m4 serial 28
+# realloc.m4 serial 29
 dnl Copyright (C) 2007, 2009-2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -16,7 +16,8 @@ AC_DEFUN([_AC_FUNC_REALLOC_IF]
           [[#include <stdlib.h>
           ]],
           [[void *p = realloc (0, 0);
-            int result = !p;
+            void * volatile vp = p;
+            int result = !vp;
             free (p);
             return result;]])
        ],




Reply via email to