Re: [PATCH] free-posix: remove stray wchar-h dependency

Bruno Haible via Gnulib discussion list <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <4772178.cEBGB3zze1@cagnes>
On 2026-05-30 Paul Eggert did:
>  2026-05-30  Paul Eggert  <[email protected]>
>  
> +	free-posix: remove stray wchar-h dependency
> +	* modules/free-posix (Depends-on): Don’t depend on wchar-h.
> +	Discovered because gzip unnecessarily brought in wchar-h machinery.

This change is a regression. It causes bogus -Wmismatched-dealloc warnings
to appear in a program that
  - uses the 'free-posix' module,
  - does not use the 'wcsdup' or 'wchar-h' modules,
  - uses the 'wcsdup' function,
  - is built on a platform where an rpl_free function is needed,
  - is built with gcc 11.


How to reproduce the problem:

1. Create three testdirs:
$ ./gnulib-tool --create-testdir --dir=../testdir3 --avoid=string-h free-posix
$ ./gnulib-tool --create-testdir --dir=../testdir4 free-posix
$ ./gnulib-tool --create-testdir --dir=../testdir5 free-posix wchar-h

2, Build each one:
$ gl_cv_func_free_preserves_errno=no ./configure; make

3. Compile the attached program that uses strdup() and wcsdup():

$ gcc-11.5.0 -O2 -Wall -Wmismatched-dealloc foo.c \
  -I/GNULIB/testdir3 -I/GNULIB/testdir3/gllib /GNULIB/testdir3/gllib/libgnu.a 
etc.

Results:

* In testdir3, which has no built string.h nor wchar.h file, there are
bogus warnings about strdup and wcsdup:

$ gcc-version 11.5.0 -O2 -Wall -Wmismatched-dealloc foo.c \
  -I/GNULIB/testdir3 -I/GNULIB/testdir3/gllib /GNULIB/testdir3/gllib/libgnu.a 
In file included from foo.c:3:
foo.c: In function ‘main’:
foo.c:12:5: warning: ‘rpl_free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
   12 |     free (s);
      |     ^
foo.c:10:15: note: returned from ‘strdup’
   10 |     char *s = strdup ("foo");
      |               ^~~~~~~~~~~~~~
In file included from foo.c:3:
foo.c:17:5: warning: ‘rpl_free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
   17 |     free (s);
      |     ^
foo.c:15:18: note: returned from ‘wcsdup’
   15 |     wchar_t *s = wcsdup (L"foo");
      |                  ^~~~~~~~~~~~~~~

* In testdir4, which has a built string.h but no built wchar.h file, there is
a bogus warning about wcsdup:

$ gcc-version 11.5.0 -O2 -Wall -Wmismatched-dealloc foo.c \
  -I/GNULIB/testdir4 -I/GNULIB/testdir4/gllib /GNULIB/testdir4/gllib/libgnu.a 
In file included from foo.c:3:
foo.c: In function ‘main’:
foo.c:17:5: warning: ‘rpl_free’ called on pointer returned from a mismatched allocation function [-Wmismatched-dealloc]
   17 |     free (s);
      |     ^
foo.c:15:18: note: returned from ‘wcsdup’
   15 |     wchar_t *s = wcsdup (L"foo");
      |                  ^~~~~~~~~~~~~~~

* In testdir5, which has a built string.h and wchar.h, no bogus warnings
appear:

$ gcc-version 11.5.0 -O2 -Wall -Wmismatched-dealloc foo.c \
  -I/GNULIB/testdir5 -I/GNULIB/testdir5/gllib /GNULIB/testdir5/gllib/libgnu.a 


Note: I don't get any -Wmismatched-dealloc warnings in this test case
with gcc versions >= 12, but I think this is a consequence of a gcc bug:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126172 .


Paul: Your motivation for this patch was to avoid a wchar.h in the gzip build.
Assuming that gzip does not use the wcsdup() function, the right fix for gzip
is to use the gnulib-tool option '--avoid=wchar-h' (in bootstrap.conf).


2026-07-08  Bruno Haible  <[email protected]>

	free-posix: Fix bogus -Wmismatched-dealloc warnings (regr. 2026-05-30).
	* modules/free-posix (Depends-on): Add back wchar-h.

diff --git a/modules/free-posix b/modules/free-posix
index 703fb7db89..317f48bafd 100644
--- a/modules/free-posix
+++ b/modules/free-posix
@@ -9,6 +9,7 @@ m4/musl.m4
 Depends-on:
 stdlib-h
 string-h
+wchar-h
 
 configure.ac:
 gl_FUNC_FREE
foo.c (text/x-csrc, 250 B)
#include <config.h>

#include <stdlib.h>
#include <string.h>
#include <wchar.h>

int main ()
{
  {
    char *s = strdup ("foo");
    s[1] = 'x';
    free (s);
  }
  {
    wchar_t *s = wcsdup (L"foo");
    s[1] = L'x';
    free (s);
  }
  return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.