bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] string, wchar: avoid some namespace pollution
@ 2021-09-08  0:56 Paul Eggert
  2021-09-08 15:14 ` Bruno Haible
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Paul Eggert @ 2021-09-08  0:56 UTC (permalink / raw)
  To: bug-gnulib, landfillbaby69; +Cc: Paul Eggert

* lib/string.in.h, lib/wchar.in.h:
(free): Declare by hand instead of including stdlib.h.
This avoids some namespace pollution.  It should also avoid some
nested-include problems described by Lucy Phipps in:
https://lists.gnu.org/r/bug-gnulib/2021-09/msg00018.html
* modules/string, modules/wchar:
(Depends-on): Add stdlib, so that REPLACE_FREE has the right value.
(Makefile.am): Replace @REPLACE_FREE@ when creating the include file.
---
 ChangeLog           | 12 ++++++++++++
 lib/attribute.h     |  2 +-
 lib/stdlib.in.h     |  2 +-
 lib/string.in.h     |  9 ++++++---
 lib/wchar.in.h      |  9 ++++++---
 m4/gnulib-common.m4 |  2 +-
 modules/string      |  2 ++
 modules/wchar       |  2 ++
 8 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 39a892e28..f73dc5a13 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2021-09-07  Paul Eggert  <eggert@cs.ucla.edu>
+
+	string, wchar: avoid some namespace pollution
+	* lib/string.in.h, lib/wchar.in.h:
+	(free): Declare by hand instead of including stdlib.h.
+	This avoids some namespace pollution.  It should also avoid some
+	nested-include problems described by Lucy Phipps in:
+	https://lists.gnu.org/r/bug-gnulib/2021-09/msg00018.html
+	* modules/string, modules/wchar:
+	(Depends-on): Add stdlib, so that REPLACE_FREE has the right value.
+	(Makefile.am): Replace @REPLACE_FREE@ when creating the include file.
+
 2021-09-04  Paul Eggert  <eggert@cs.ucla.edu>
 
 	idx: break copying from glibc
diff --git a/lib/attribute.h b/lib/attribute.h
index 26a555655..eb36188d4 100644
--- a/lib/attribute.h
+++ b/lib/attribute.h
@@ -80,7 +80,7 @@
    that can be freed by passing them as the Ith argument to the
    function F.
    ATTRIBUTE_DEALLOC_FREE declares that the function returns pointers that
-   can be freed via 'free'; it can be used only after including <stdlib.h>.  */
+   can be freed via 'free'; it can be used only after declaring 'free'.  */
 /* Applies to: functions.  Cannot be used on inline functions.  */
 #define ATTRIBUTE_DEALLOC(f, i) _GL_ATTRIBUTE_DEALLOC(f, i)
 #define ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC_FREE
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index d0ea07f8b..0855112d1 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -111,7 +111,7 @@ struct random_data
 #endif
 
 /* _GL_ATTRIBUTE_DEALLOC_FREE declares that the function returns pointers that
-   can be freed via 'free'; it can be used only after including <stdlib.h>.  */
+   can be freed via 'free'; it can be used only after declaring 'free'.  */
 /* Applies to: functions.  Cannot be used on inline functions.  */
 #ifndef _GL_ATTRIBUTE_DEALLOC_FREE
 # define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (free, 1)
diff --git a/lib/string.in.h b/lib/string.in.h
index fa2e40c25..6214b5578 100644
--- a/lib/string.in.h
+++ b/lib/string.in.h
@@ -47,9 +47,6 @@
 /* NetBSD 5.0 mis-defines NULL.  */
 #include <stddef.h>
 
-/* Get free().  */
-#include <stdlib.h>
-
 /* MirBSD defines mbslen as a macro.  */
 #if @GNULIB_MBSLEN@ && defined __MirBSD__
 # include <wchar.h>
@@ -86,6 +83,12 @@
 
 /* The definition of _GL_WARN_ON_USE is copied here.  */
 
+/* Declare 'free' if needed for _GL_ATTRIBUTE_DEALLOC_FREE.  */
+#if (@REPLACE_FREE@ && !defined free \
+     && !(defined __cplusplus && defined GNULIB_NAMESPACE))
+# define free rpl_free
+#endif
+_GL_EXTERN_C void free (void *);
 
 /* Clear a block of memory.  The compiler will not delete a call to
    this function, even if the block is dead after the call.  */
diff --git a/lib/wchar.in.h b/lib/wchar.in.h
index be5d36c8d..027a14549 100644
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -72,9 +72,6 @@
 # include <stddef.h>
 #endif
 
-/* Get free().  */
-#include <stdlib.h>
-
 /* Include the original <wchar.h> if it exists.
    Some builds of uClibc lack it.  */
 /* The include_next requires a split double-inclusion guard.  */
@@ -149,6 +146,12 @@ typedef int rpl_mbstate_t;
 # endif
 #endif
 
+/* Declare 'free' if needed for _GL_ATTRIBUTE_DEALLOC_FREE.  */
+#if (@REPLACE_FREE@ && !defined free \
+     && !(defined __cplusplus && defined GNULIB_NAMESPACE))
+# define free rpl_free
+#endif
+_GL_EXTERN_C void free (void *);
 
 /* Convert a single-byte character to a wide character.  */
 #if @GNULIB_BTOWC@
diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index 5d667052d..12b19dbcb 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -174,7 +174,7 @@ AC_DEFUN([gl_COMMON_BODY], [
    that can be freed by passing them as the Ith argument to the
    function F.
    _GL_ATTRIBUTE_DEALLOC_FREE declares that the function returns pointers that
-   can be freed via 'free'; it can be used only after including <stdlib.h>.  */
+   can be freed via 'free'; it can be used only after declaring 'free'.  */
 /* Applies to: functions.  Cannot be used on inline functions.  */
 #if _GL_GNUC_PREREQ (11, 0)
 # define _GL_ATTRIBUTE_DEALLOC(f, i) __attribute__ ((__malloc__ (f, i)))
diff --git a/modules/string b/modules/string
index e1d9980bb..306834591 100644
--- a/modules/string
+++ b/modules/string
@@ -13,6 +13,7 @@ snippet/arg-nonnull
 snippet/c++defs
 snippet/warn-on-use
 stddef
+stdlib
 
 configure.ac:
 gl_STRING_H
@@ -102,6 +103,7 @@ string.h: string.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H
 	      -e 's|@''REPLACE_FFSLL''@|$(REPLACE_FFSLL)|g' \
 	      -e 's|@''REPLACE_MEMCHR''@|$(REPLACE_MEMCHR)|g' \
 	      -e 's|@''REPLACE_MEMMEM''@|$(REPLACE_MEMMEM)|g' \
+	      -e 's|@''REPLACE_FREE''@|$(REPLACE_FREE)|g' \
 	      -e 's|@''REPLACE_STPNCPY''@|$(REPLACE_STPNCPY)|g' \
 	      -e 's|@''REPLACE_STRCHRNUL''@|$(REPLACE_STRCHRNUL)|g' \
 	      -e 's|@''REPLACE_STRDUP''@|$(REPLACE_STRDUP)|g' \
diff --git a/modules/wchar b/modules/wchar
index d4e6d6933..d34cb6a22 100644
--- a/modules/wchar
+++ b/modules/wchar
@@ -14,6 +14,7 @@ snippet/c++defs
 snippet/warn-on-use
 inttypes-incomplete
 stddef
+stdlib
 
 configure.ac:
 gl_WCHAR_H
@@ -126,6 +127,7 @@ wchar.h: wchar.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
 	  sed -e 's|@''REPLACE_MBSTATE_T''@|$(REPLACE_MBSTATE_T)|g' \
 	      -e 's|@''REPLACE_BTOWC''@|$(REPLACE_BTOWC)|g' \
 	      -e 's|@''REPLACE_WCTOB''@|$(REPLACE_WCTOB)|g' \
+	      -e 's|@''REPLACE_FREE''@|$(REPLACE_FREE)|g' \
 	      -e 's|@''REPLACE_MBSINIT''@|$(REPLACE_MBSINIT)|g' \
 	      -e 's|@''REPLACE_MBRTOWC''@|$(REPLACE_MBRTOWC)|g' \
 	      -e 's|@''REPLACE_MBRLEN''@|$(REPLACE_MBRLEN)|g' \
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-01-04 12:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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).