]> Savannah Git Hosting - gnulib.git/commitdiff
string: Fix "mismatched allocation function" warnings regarding strndup.
authorBruno Haible <bruno@clisp.org>
Thu, 10 Feb 2022 01:44:14 +0000 (02:44 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 10 Feb 2022 01:44:14 +0000 (02:44 +0100)
* lib/string.in.h (strndup): Mark with _GL_ATTRIBUTE_MALLOC and
_GL_ATTRIBUTE_DEALLOC_FREE. For GCC >= 11: Declare also when the
platform already declares the function or when the module 'strndup' is
not in use.

ChangeLog
lib/string.in.h

index 4208bb1372e462c3fbc1564021d03b296a44cdd1..62162cbfce6ed435a46ebfe1cd484b666ee8bd65 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-02-09  Bruno Haible  <bruno@clisp.org>
+
+       string: Fix "mismatched allocation function" warnings regarding strndup.
+       * lib/string.in.h (strndup): Mark with _GL_ATTRIBUTE_MALLOC and
+       _GL_ATTRIBUTE_DEALLOC_FREE. For GCC >= 11: Declare also when the
+       platform already declares the function or when the module 'strndup' is
+       not in use.
+
 2022-02-09  Bruno Haible  <bruno@clisp.org>
 
        unictype/category-byname: Fix test failure.
index 03e6a17a36dcfd17f39f1f316e8caf685c36dfa3..c9432948c157e30747ac8e3d3be652d960b4a018 100644 (file)
@@ -563,22 +563,35 @@ _GL_WARN_ON_USE (strncat, "strncat is unportable - "
 #   undef strndup
 #   define strndup rpl_strndup
 #  endif
-_GL_FUNCDECL_RPL (strndup, char *, (char const *__s, size_t __n)
-                                   _GL_ARG_NONNULL ((1)));
+_GL_FUNCDECL_RPL (strndup, char *,
+                  (char const *__s, size_t __n)
+                  _GL_ARG_NONNULL ((1))
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 _GL_CXXALIAS_RPL (strndup, char *, (char const *__s, size_t __n));
 # else
-#  if ! @HAVE_DECL_STRNDUP@
-_GL_FUNCDECL_SYS (strndup, char *, (char const *__s, size_t __n)
-                                   _GL_ARG_NONNULL ((1)));
+#  if !@HAVE_DECL_STRNDUP@ || __GNUC__ >= 11
+_GL_FUNCDECL_SYS (strndup, char *,
+                  (char const *__s, size_t __n)
+                  _GL_ARG_NONNULL ((1))
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
 #  endif
 _GL_CXXALIAS_SYS (strndup, char *, (char const *__s, size_t __n));
 # endif
 _GL_CXXALIASWARN (strndup);
-#elif defined GNULIB_POSIXCHECK
-# undef strndup
-# if HAVE_RAW_DECL_STRNDUP
+#else
+# if __GNUC__ >= 11
+/* For -Wmismatched-dealloc: Associate strndup with free or rpl_free.  */
+_GL_FUNCDECL_SYS (strndup, char *,
+                  (char const *__s, size_t __n)
+                  _GL_ARG_NONNULL ((1))
+                  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE);
+# endif
+# if defined GNULIB_POSIXCHECK
+#  undef strndup
+#  if HAVE_RAW_DECL_STRNDUP
 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
                  "use gnulib module strndup for portability");
+#  endif
 # endif
 #endif