From d57726cabd19ee22e9ab9c00ca53bc7eaa368f8e Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 7 Aug 2021 18:05:44 +0200 Subject: [PATCH] dirent: Improve GCC 11 allocation-deallocation checking. * lib/dirent.in.h (closedir): Move declaration up. (opendir, fdopendir): Declare that that deallocation must happen through 'closedir'. * lib/dirent-safer.h (opendir_safer): Likewise. * lib/opendirat.h (opendirat): Likewise. --- ChangeLog | 9 +++++++ lib/dirent-safer.h | 3 ++- lib/dirent.in.h | 66 ++++++++++++++++++++++++++-------------------- lib/opendirat.h | 3 ++- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index c94dda2da3..fbfa848a9b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2021-08-07 Bruno Haible + + dirent: Improve GCC 11 allocation-deallocation checking. + * lib/dirent.in.h (closedir): Move declaration up. + (opendir, fdopendir): Declare that that deallocation must happen through + 'closedir'. + * lib/dirent-safer.h (opendir_safer): Likewise. + * lib/opendirat.h (opendirat): Likewise. + 2021-08-07 Bruno Haible stdlib, string, wchar: Improve GCC 11 allocation-deallocation checking. diff --git a/lib/dirent-safer.h b/lib/dirent-safer.h index e18bb89d36..70ada062ec 100644 --- a/lib/dirent-safer.h +++ b/lib/dirent-safer.h @@ -23,7 +23,8 @@ extern "C" { #endif -DIR *opendir_safer (const char *name); +DIR *opendir_safer (const char *name) + _GL_ATTRIBUTE_DEALLOC (closedir, 1); #ifdef __cplusplus } diff --git a/lib/dirent.in.h b/lib/dirent.in.h index 647a3dba51..49e0aa90ea 100644 --- a/lib/dirent.in.h +++ b/lib/dirent.in.h @@ -74,6 +74,30 @@ typedef struct gl_directory DIR; /* Declare overridden functions. */ +#if @GNULIB_CLOSEDIR@ +# if @REPLACE_CLOSEDIR@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef closedir +# define closedir rpl_closedir +# define GNULIB_defined_closedir 1 +# endif +_GL_FUNCDECL_RPL (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1))); +_GL_CXXALIAS_RPL (closedir, int, (DIR *dirp)); +# else +# if !@HAVE_CLOSEDIR@ +_GL_FUNCDECL_SYS (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1))); +# endif +_GL_CXXALIAS_SYS (closedir, int, (DIR *dirp)); +# endif +_GL_CXXALIASWARN (closedir); +#elif defined GNULIB_POSIXCHECK +# undef closedir +# if HAVE_RAW_DECL_CLOSEDIR +_GL_WARN_ON_USE (closedir, "closedir is not portable - " + "use gnulib module closedir for portability"); +# endif +#endif + #if @GNULIB_OPENDIR@ # if @REPLACE_OPENDIR@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) @@ -81,11 +105,17 @@ typedef struct gl_directory DIR; # define opendir rpl_opendir # define GNULIB_defined_opendir 1 # endif -_GL_FUNCDECL_RPL (opendir, DIR *, (const char *dir_name) _GL_ARG_NONNULL ((1))); +_GL_FUNCDECL_RPL (opendir, DIR *, + (const char *dir_name) + _GL_ARG_NONNULL ((1)) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); _GL_CXXALIAS_RPL (opendir, DIR *, (const char *dir_name)); # else # if !@HAVE_OPENDIR@ -_GL_FUNCDECL_SYS (opendir, DIR *, (const char *dir_name) _GL_ARG_NONNULL ((1))); +_GL_FUNCDECL_SYS (opendir, DIR *, + (const char *dir_name) + _GL_ARG_NONNULL ((1)) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); # endif _GL_CXXALIAS_SYS (opendir, DIR *, (const char *dir_name)); # endif @@ -126,30 +156,6 @@ _GL_WARN_ON_USE (rewinddir, "rewinddir is not portable - " # endif #endif -#if @GNULIB_CLOSEDIR@ -# if @REPLACE_CLOSEDIR@ -# if !(defined __cplusplus && defined GNULIB_NAMESPACE) -# undef closedir -# define closedir rpl_closedir -# define GNULIB_defined_closedir 1 -# endif -_GL_FUNCDECL_RPL (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1))); -_GL_CXXALIAS_RPL (closedir, int, (DIR *dirp)); -# else -# if !@HAVE_CLOSEDIR@ -_GL_FUNCDECL_SYS (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1))); -# endif -_GL_CXXALIAS_SYS (closedir, int, (DIR *dirp)); -# endif -_GL_CXXALIASWARN (closedir); -#elif defined GNULIB_POSIXCHECK -# undef closedir -# if HAVE_RAW_DECL_CLOSEDIR -_GL_WARN_ON_USE (closedir, "closedir is not portable - " - "use gnulib module closedir for portability"); -# endif -#endif - #if @GNULIB_DIRFD@ /* Return the file descriptor associated with the given directory stream, or -1 if none exists. */ @@ -200,11 +206,15 @@ _GL_WARN_ON_USE (dirfd, "dirfd is unportable - " # undef fdopendir # define fdopendir rpl_fdopendir # endif -_GL_FUNCDECL_RPL (fdopendir, DIR *, (int fd)); +_GL_FUNCDECL_RPL (fdopendir, DIR *, + (int fd) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); _GL_CXXALIAS_RPL (fdopendir, DIR *, (int fd)); # else # if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@ -_GL_FUNCDECL_SYS (fdopendir, DIR *, (int fd)); +_GL_FUNCDECL_SYS (fdopendir, DIR *, + (int fd) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); # endif _GL_CXXALIAS_SYS (fdopendir, DIR *, (int fd)); # endif diff --git a/lib/opendirat.h b/lib/opendirat.h index 845cc552f4..9765615b68 100644 --- a/lib/opendirat.h +++ b/lib/opendirat.h @@ -17,4 +17,5 @@ #include -DIR *opendirat (int, char const *, int, int *); +DIR *opendirat (int, char const *, int, int *) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1); -- 2.39.5