bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org, Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: [PATCH] string, wchar: avoid some namespace pollution
Date: Tue, 04 Jan 2022 13:22:33 +0100	[thread overview]
Message-ID: <12932125.s0nyknJP1Z@omega> (raw)
In-Reply-To: <20210908005632.100624-1-eggert@cs.ucla.edu>

Paul Eggert did this on 2021-09-07:
> * lib/string.in.h, lib/wchar.in.h:
> (free): Declare by hand instead of including stdlib.h.
> This avoids some namespace pollution.

Unfortunately, when compiling GNU libiconv (or GNU libunistring)
with MSVC, it causes this compilation error:

/home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -I. -I../../srclib -I.. -I../lib  -DDEPENDS_ON_LIBICONV=1 -DDEPENDS_ON_LIBINTL=1 -D_WIN32_WINNT=_WIN32_WINNT_WINXP -I/usr/local/msvc32/include  -MD -c -o stat.obj `cygpath -w '../../srclib/stat.c'`
stat.c
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\corecrt_malloc.h(85): error C2375: 'rpl_free': redefinition; different linkage
.\string.h(628): note: see declaration of 'rpl_free'

This was first reported in <https://stackoverflow.com/questions/70525755/> .

The problem is that if <stdlib.h> is not included at a certain point,
and we fiddle around with the 'free' declaration, and <stdlib.h> is
included later, we can easily get a compilation error due to inconsistent
declarations of free().

This patch fixes it.


2022-01-04  Bruno Haible  <bruno@clisp.org>

	string, wchar: Fix compilation error on MSVC (regression 2021-09-07).
	* lib/string.in.h (free): Don't redeclare as rpl_free. Instead, redefine
	_GL_ATTRIBUTE_DEALLOC_FREE to reference rpl_free directly.
	* lib/wchar.in.h (free): Likewise.

diff --git a/lib/string.in.h b/lib/string.in.h
index 0c4aefcce..03e6a17a3 100644
--- a/lib/string.in.h
+++ b/lib/string.in.h
@@ -112,12 +112,26 @@
 
 /* The definition of _GL_WARN_ON_USE is copied here.  */
 
-/* Declare 'free' if needed for _GL_ATTRIBUTE_DEALLOC_FREE.  */
-_GL_EXTERN_C void free (void *);
+/* Make _GL_ATTRIBUTE_DEALLOC_FREE work, even though <stdlib.h> may not have
+   been included yet.  */
 #if @GNULIB_FREE_POSIX@
 # if (@REPLACE_FREE@ && !defined free \
       && !(defined __cplusplus && defined GNULIB_NAMESPACE))
-#  define free rpl_free
+/* We can't do '#define free rpl_free' here.  */
+_GL_EXTERN_C void rpl_free (void *);
+#  undef _GL_ATTRIBUTE_DEALLOC_FREE
+#  define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (rpl_free, 1)
+# else
+#  if defined _MSC_VER
+_GL_EXTERN_C void __cdecl free (void *);
+#  else
+_GL_EXTERN_C void free (void *);
+#  endif
+# endif
+#else
+# if defined _MSC_VER
+_GL_EXTERN_C void __cdecl free (void *);
+# else
 _GL_EXTERN_C void free (void *);
 # endif
 #endif
diff --git a/lib/wchar.in.h b/lib/wchar.in.h
index 4db98741a..d7792e5fb 100644
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -175,12 +175,26 @@ typedef int rpl_mbstate_t;
 # endif
 #endif
 
-/* Declare 'free' if needed for _GL_ATTRIBUTE_DEALLOC_FREE.  */
-_GL_EXTERN_C void free (void *);
+/* Make _GL_ATTRIBUTE_DEALLOC_FREE work, even though <stdlib.h> may not have
+   been included yet.  */
 #if @GNULIB_FREE_POSIX@
 # if (@REPLACE_FREE@ && !defined free \
       && !(defined __cplusplus && defined GNULIB_NAMESPACE))
-#  define free rpl_free
+/* We can't do '#define free rpl_free' here.  */
+_GL_EXTERN_C void rpl_free (void *);
+#  undef _GL_ATTRIBUTE_DEALLOC_FREE
+#  define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (rpl_free, 1)
+# else
+#  if defined _MSC_VER
+_GL_EXTERN_C void __cdecl free (void *);
+#  else
+_GL_EXTERN_C void free (void *);
+#  endif
+# endif
+#else
+# if defined _MSC_VER
+_GL_EXTERN_C void __cdecl free (void *);
+# else
 _GL_EXTERN_C void free (void *);
 # endif
 #endif





      parent reply	other threads:[~2022-01-04 12:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-08  0:56 [PATCH] string, wchar: avoid some namespace pollution Paul Eggert
2021-09-08 15:14 ` Bruno Haible
2021-09-08 15:40   ` Bruno Haible
2021-09-18 14:49 ` Bruno Haible
2021-09-23 14:44   ` Simon Josefsson via Gnulib discussion list
2022-01-04 12:22 ` Bruno Haible [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12932125.s0nyknJP1Z@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).