]> Savannah Git Hosting - gnulib.git/commitdiff
stdlib, string, wchar: Improve GCC 11 allocation-deallocation checking.
authorBruno Haible <bruno@clisp.org>
Sat, 7 Aug 2021 14:40:12 +0000 (16:40 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 7 Aug 2021 14:40:12 +0000 (16:40 +0200)
* lib/stdlib.in.h (free): Move declaration up.
(aligned_alloc, calloc, canonicalize_file_name, malloc, realloc):
Declare that deallocation must happen through 'free' (which may actually
be 'rpl_free').
* lib/string.in.h: Include <stdlib.h>.
(strdup): Declare deallocation.
* lib/wchar.in.h: Include <stdlib.h>.
(wcsdup): Declare deallocation.
* lib/malloc.in.h: Include <stdlib.h>.
(memalign): Declare deallocation.

ChangeLog
lib/malloc.in.h
lib/stdlib.in.h
lib/string.in.h
lib/wchar.in.h

index f7a59b4941b46015f415b6b277ae39ba60c4e60f..c94dda2da3fba84b7cd29269d2ae967e23812767 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2021-08-07  Bruno Haible  <bruno@clisp.org>
+
+       stdlib, string, wchar: Improve GCC 11 allocation-deallocation checking.
+       * lib/stdlib.in.h (free): Move declaration up.
+       (aligned_alloc, calloc, canonicalize_file_name, malloc, realloc):
+       Declare that deallocation must happen through 'free' (which may actually
+       be 'rpl_free').
+       * lib/string.in.h: Include <stdlib.h>.
+       (strdup): Declare deallocation.
+       * lib/wchar.in.h: Include <stdlib.h>.
+       (wcsdup): Declare deallocation.
+       * lib/malloc.in.h: Include <stdlib.h>.
+       (memalign): Declare deallocation.
+
 2021-08-07  Bruno Haible  <bruno@clisp.org>
 
        year2038: Fix recommendation regarding -m64 flag.
index 851abb1875a3b4a136b3d622ad265a9b160f7ad4..a7394d2b2b0cb1fad52c61385054bd9e98cad08d 100644 (file)
 #endif
 @PRAGMA_COLUMNS@
 
+#if @GNULIB_MEMALIGN@
+# include <stdlib.h> /* for free() */
+#endif
+
 /* The include_next requires a split double-inclusion guard.  */
 #if @HAVE_MALLOC_H@
 # @INCLUDE_NEXT@ @NEXT_MALLOC_H@
@@ -46,7 +50,9 @@
 #   undef memalign
 #   define memalign rpl_memalign
 #  endif
-_GL_FUNCDECL_RPL (memalign, void *, (size_t alignment, size_t size));
+_GL_FUNCDECL_RPL (memalign, void *,
+                  (size_t alignment, size_t size)
+                  _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (memalign, void *, (size_t alignment, size_t size));
 # else
 #  if @HAVE_MEMALIGN@
index 652062d949c2201509b2d46041bd37d2390961a2..0e030f11a8aab109e53d66da5b3a7e0efaa34fd8 100644 (file)
@@ -149,6 +149,28 @@ _GL_WARN_ON_USE (_Exit, "_Exit is unportable - "
 #endif
 
 
+#if @GNULIB_FREE_POSIX@
+# if @REPLACE_FREE@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef free
+#   define free rpl_free
+#  endif
+_GL_FUNCDECL_RPL (free, void, (void *ptr));
+_GL_CXXALIAS_RPL (free, void, (void *ptr));
+# else
+_GL_CXXALIAS_SYS (free, void, (void *ptr));
+# endif
+# if __GLIBC__ >= 2
+_GL_CXXALIASWARN (free);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef free
+/* Assume free is always declared.  */
+_GL_WARN_ON_USE (free, "free is not future POSIX compliant everywhere - "
+                 "use gnulib module free for portability");
+#endif
+
+
 /* Allocate memory with indefinite extent and specified alignment.  */
 #if @GNULIB_ALIGNED_ALLOC@
 # if @REPLACE_ALIGNED_ALLOC@
@@ -156,7 +178,9 @@ _GL_WARN_ON_USE (_Exit, "_Exit is unportable - "
 #   undef aligned_alloc
 #   define aligned_alloc rpl_aligned_alloc
 #  endif
-_GL_FUNCDECL_RPL (aligned_alloc, void *, (size_t alignment, size_t size));
+_GL_FUNCDECL_RPL (aligned_alloc, void *,
+                  (size_t alignment, size_t size)
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (aligned_alloc, void *, (size_t alignment, size_t size));
 # else
 #  if @HAVE_ALIGNED_ALLOC@
@@ -198,9 +222,17 @@ _GL_WARN_ON_USE (atoll, "atoll is unportable - "
 #   undef calloc
 #   define calloc rpl_calloc
 #  endif
-_GL_FUNCDECL_RPL (calloc, void *, (size_t nmemb, size_t size));
+_GL_FUNCDECL_RPL (calloc, void *,
+                  (size_t nmemb, size_t size)
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (calloc, void *, (size_t nmemb, size_t size));
 # else
+#  if @GNULIB_FREE_POSIX@ && @REPLACE_FREE@ && __GNUC__ >= 11
+/* For -Wmismatched-dealloc: Associate calloc with rpl_free.  */
+_GL_FUNCDECL_SYS (calloc, void *,
+                  (size_t nmemb, size_t size)
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
+#  endif
 _GL_CXXALIAS_SYS (calloc, void *, (size_t nmemb, size_t size));
 # endif
 # if __GLIBC__ >= 2
@@ -218,13 +250,17 @@ _GL_WARN_ON_USE (calloc, "calloc is not POSIX compliant everywhere - "
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
 #   define canonicalize_file_name rpl_canonicalize_file_name
 #  endif
-_GL_FUNCDECL_RPL (canonicalize_file_name, char *, (const char *name)
-                                                  _GL_ARG_NONNULL ((1)));
+_GL_FUNCDECL_RPL (canonicalize_file_name, char *,
+                  (const char *name)
+                  _GL_ARG_NONNULL ((1))
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (canonicalize_file_name, char *, (const char *name));
 # else
 #  if !@HAVE_CANONICALIZE_FILE_NAME@
-_GL_FUNCDECL_SYS (canonicalize_file_name, char *, (const char *name)
-                                                  _GL_ARG_NONNULL ((1)));
+_GL_FUNCDECL_SYS (canonicalize_file_name, char *,
+                  (const char *name)
+                  _GL_ARG_NONNULL ((1))
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 #  endif
 _GL_CXXALIAS_SYS (canonicalize_file_name, char *, (const char *name));
 # endif
@@ -288,27 +324,6 @@ _GL_CXXALIASWARN (fcvt);
 # endif
 #endif
 
-#if @GNULIB_FREE_POSIX@
-# if @REPLACE_FREE@
-#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
-#   undef free
-#   define free rpl_free
-#  endif
-_GL_FUNCDECL_RPL (free, void, (void *ptr));
-_GL_CXXALIAS_RPL (free, void, (void *ptr));
-# else
-_GL_CXXALIAS_SYS (free, void, (void *ptr));
-# endif
-# if __GLIBC__ >= 2
-_GL_CXXALIASWARN (free);
-# endif
-#elif defined GNULIB_POSIXCHECK
-# undef free
-/* Assume free is always declared.  */
-_GL_WARN_ON_USE (free, "free is not future POSIX compliant everywhere - "
-                 "use gnulib module free for portability");
-#endif
-
 #if @GNULIB_MDA_GCVT@
 /* On native Windows, map 'gcvt' to '_gcvt', so that -loldnames is not
    required.  In C++ with GNULIB_NAMESPACE, avoid differences between
@@ -404,9 +419,17 @@ _GL_WARN_ON_USE (grantpt, "grantpt is not portable - "
 #   undef malloc
 #   define malloc rpl_malloc
 #  endif
-_GL_FUNCDECL_RPL (malloc, void *, (size_t size));
+_GL_FUNCDECL_RPL (malloc, void *,
+                  (size_t size)
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (malloc, void *, (size_t size));
 # else
+#  if @GNULIB_FREE_POSIX@ && @REPLACE_FREE@ && __GNUC__ >= 11
+/* For -Wmismatched-dealloc: Associate malloc with rpl_free.  */
+_GL_FUNCDECL_SYS (malloc, void *,
+                  (size_t size)
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
+#  endif
 _GL_CXXALIAS_SYS (malloc, void *, (size_t size));
 # endif
 # if __GLIBC__ >= 2
@@ -1015,9 +1038,15 @@ _GL_WARN_ON_USE (setstate_r, "setstate_r is unportable - "
 #   undef realloc
 #   define realloc rpl_realloc
 #  endif
-_GL_FUNCDECL_RPL (realloc, void *, (void *ptr, size_t size));
+_GL_FUNCDECL_RPL (realloc, void *, (void *ptr, size_t size)
+                                   _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (realloc, void *, (void *ptr, size_t size));
 # else
+#  if @GNULIB_FREE_POSIX@ && @REPLACE_FREE@ && __GNUC__ >= 11
+/* For -Wmismatched-dealloc: Associate realloc with rpl_free.  */
+_GL_FUNCDECL_SYS (realloc, void *, (void *ptr, size_t size)
+                                   _GL_ATTRIBUTE_DEALLOC_FREE);
+#  endif
 _GL_CXXALIAS_SYS (realloc, void *, (void *ptr, size_t size));
 # endif
 # if __GLIBC__ >= 2
index cb964bc8d1cf35558d44a495210c3d13edad2e6a..c67c8c06eb28eca40334485f5db986dbf4f1f1f3 100644 (file)
 /* NetBSD 5.0 mis-defines NULL.  */
 #include <stddef.h>
 
+#if @GNULIB_STRDUP@
+# include <stdlib.h> /* for free() */
+#endif
+
 /* MirBSD defines mbslen as a macro.  */
 #if @GNULIB_MBSLEN@ && defined __MirBSD__
 # include <wchar.h>
@@ -418,7 +422,10 @@ _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
 #   undef strdup
 #   define strdup rpl_strdup
 #  endif
-_GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
+_GL_FUNCDECL_RPL (strdup, char *,
+                  (char const *__s)
+                  _GL_ARG_NONNULL ((1))
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
 # elif defined _WIN32 && !defined __CYGWIN__
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
@@ -432,7 +439,10 @@ _GL_CXXALIAS_MDA (strdup, char *, (char const *__s));
 #   undef strdup
 #  endif
 #  if !(@HAVE_DECL_STRDUP@ || defined strdup)
-_GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
+_GL_FUNCDECL_SYS (strdup, char *,
+                  (char const *__s)
+                  _GL_ARG_NONNULL ((1))
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 #  endif
 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
 # endif
index 919a5a37e934f30728dd41b826df75201396305b..64e55381efc309ec3dcaf2892b2b79270ca66db1 100644 (file)
 # include <stddef.h>
 #endif
 
+#if @GNULIB_WCSDUP@
+# include <stdlib.h> /* for free() */
+#endif
+
 /* Include the original <wchar.h> if it exists.
    Some builds of uClibc lack it.  */
 /* The include_next requires a split double-inclusion guard.  */
@@ -942,7 +946,9 @@ _GL_WARN_ON_USE (wcsxfrm, "wcsxfrm is unportable - "
 _GL_CXXALIAS_MDA (wcsdup, wchar_t *, (const wchar_t *s));
 # else
 #  if !@HAVE_WCSDUP@
-_GL_FUNCDECL_SYS (wcsdup, wchar_t *, (const wchar_t *s));
+_GL_FUNCDECL_SYS (wcsdup, wchar_t *,
+                  (const wchar_t *s)
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 #  endif
 _GL_CXXALIAS_SYS (wcsdup, wchar_t *, (const wchar_t *s));
 # endif