[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
string: Fix "mismatched allocation function" warnings regarding strndup
From: |
Bruno Haible |
Subject: |
string: Fix "mismatched allocation function" warnings regarding strndup |
Date: |
Thu, 10 Feb 2022 02:48:27 +0100 |
When compiling GNU poke 2.1 with GCC 11 (on Linux/x86_64), I see these
warnings:
../../libpoke/libpoke.c:221:7: warning: 'rpl_free' called on pointer returned
from a mismatched allocation function [-Wmismatched-dealloc]
../../libpoke/pkl-ast.c:700:7: warning: 'rpl_free' called on pointer returned
from a mismatched allocation function [-Wmismatched-dealloc]
The cause is that the program invokes strndup() and then frees the result
using free(), which at this point in the program macroexpands to rpl_free().
This situation should not cause a warning.
This patch fixes it, by using the same idiom for 'strndup' as for 'strdup'.
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.
diff --git a/lib/string.in.h b/lib/string.in.h
index 03e6a17a36..c9432948c1 100644
--- a/lib/string.in.h
+++ b/lib/string.in.h
@@ -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
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- string: Fix "mismatched allocation function" warnings regarding strndup,
Bruno Haible <=