Paul Eggert wrote:
> +     * lib/stdlib.in.h (_GL_INLINE_RPL_REALLOC): New macro.
> +     (rpl_realloc): Define as inline if _GL_INLINE_RPL_REALLOC.
> +     * m4/realloc.m4 (gl_FUNC_REALLOC_0_NONNULL):
> +     Set REPLACE_REALLOC_FOR_REALLOC_POSIX=2 instead of 1,

While this is a useful optimization, it causes a link error if the
'realloc-posix' module occurs as a dependency of a tests module,
and gnulib-tool option --single-configure is in use.

How to reproduce:
$ ./gnulib-tool --test --single-configure memset_explicit
...
gcc  -Wno-error -g -O2   -o test-random_r test-random_r.o libtests.a 
../gllib/libgnu.a libtests.a ../gllib/libgnu.a libtests.a  
depbase=`echo test-realloc-posix.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
gcc -DHAVE_CONFIG_H -DEXEEXT=\"\" -I. -I../../gltests -I..  
-DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. 
-I../../gltests/.. -I../gllib -I../../gltests/../gllib   -Wno-error -g -O2 -MT 
test-realloc-posix.o -MD -MP -MF $depbase.Tpo -c -o test-realloc-posix.o 
../../gltests/test-realloc-posix.c &&\
mv -f $depbase.Tpo $depbase.Po
gcc  -Wno-error -g -O2   -o test-realloc-posix test-realloc-posix.o libtests.a 
../gllib/libgnu.a libtests.a ../gllib/libgnu.a libtests.a  
/usr/bin/ld: 
test-realloc-posix.o:/media/develdata/devel/GNULIB/gnulib-git/testdir5419/build/gltests/../../gltests/test-realloc-posix.c:29:
 undefined reference to `rpl_realloc'
collect2: error: ld returned 1 exit status
make[4]: *** [Makefile:4059: test-realloc-posix] Error 1

The situation is:
- No gllib/realloc.o.
- stdlib.o does not contain rpl_realloc because in gllib/ the module
  'realloc-posix' is not present.
- No gltests/realloc.o. <== WRONG
- In gltests/ the module 'realloc-posix' is present, but realloc.c is not
  compiled. <== WRONG

This patch fixes it. It is possible that the same bug still occurs when there
are 2 or more gnulib-tool invocations in the scope of the same configure file;
still need to think about this case.

But at least, now both of
  $ ./gnulib-tool --test realloc-posix
  $ ./gnulib-tool --test --single-configure memset_explicit
work.


2024-11-05  Bruno Haible  <[email protected]>

        realloc-posix: Fix link error (regression yesterday).
        * lib/stdlib.in.h (realloc): Don't inline rpl_realloc if IN_GNULIB_TESTS
        is defined.
        * modules/realloc-posix (Depends-on, configure.ac): Compile realloc.c
        also if REPLACE_REALLOC_FOR_REALLOC_POSIX is 2.

diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index 7344157263..0d3e2cbf94 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -1461,7 +1461,7 @@ _GL_WARN_ON_USE (setstate_r, "setstate_r is unportable - "
 
 #if @GNULIB_REALLOC_POSIX@
 # if @REPLACE_REALLOC_FOR_REALLOC_POSIX@
-#  if @REPLACE_REALLOC_FOR_REALLOC_POSIX@ == 2
+#  if @REPLACE_REALLOC_FOR_REALLOC_POSIX@ == 2 && !IN_GNULIB_TESTS
 #   define _GL_INLINE_RPL_REALLOC 1
 _GL_STDLIB_INLINE void *
 rpl_realloc (void *ptr, size_t size)
@@ -1474,7 +1474,7 @@ rpl_realloc (void *ptr, size_t size)
 #   undef realloc
 #   define realloc rpl_realloc
 #  endif
-#  if @REPLACE_REALLOC_FOR_REALLOC_POSIX@ != 2
+#  if !defined _GL_INLINE_RPL_REALLOC
 _GL_FUNCDECL_RPL (realloc, void *,
                   (void *ptr, size_t size),
                   _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_NODISCARD);
diff --git a/modules/realloc-posix b/modules/realloc-posix
index b19c82f792..236afa3113 100644
--- a/modules/realloc-posix
+++ b/modules/realloc-posix
@@ -8,13 +8,13 @@ m4/malloc.m4
 
 Depends-on:
 extensions-aix
-stdckdint            [test $REPLACE_REALLOC_FOR_REALLOC_POSIX = 1]
+stdckdint            [test $REPLACE_REALLOC_FOR_REALLOC_POSIX != 0]
 stdlib
 
 configure.ac:
 gl_FUNC_REALLOC_POSIX
 gl_FUNC_REALLOC_0_NONNULL
-if test $REPLACE_REALLOC_FOR_REALLOC_POSIX = 1; then
+if test $REPLACE_REALLOC_FOR_REALLOC_POSIX != 0; then
   AC_LIBOBJ([realloc])
 fi
 gl_STDLIB_MODULE_INDICATOR([realloc-posix])




Reply via email to