From b7b0944f5200b76811e2c1fb5ebf272704d5bcee Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 10 Feb 2022 02:44:14 +0100 Subject: [PATCH] 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. --- ChangeLog | 8 ++++++++ lib/string.in.h | 29 +++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 29e49b4e09..35330808a4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2022-02-09 Bruno Haible + + 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 unictype/category-byname: Fix test failure. 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 -- 2.39.5