From: Bruno Haible Date: Thu, 10 Feb 2022 01:44:14 +0000 (+0100) Subject: string: Fix "mismatched allocation function" warnings regarding strndup. X-Git-Tag: v1.0~2358 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=cbdb5ea63cb5348d9ead16dc46bedda77a4c3d7d;p=gnulib.git 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/ChangeLog b/ChangeLog index 4208bb1372..62162cbfce 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