bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

dirent: Improve GCC 11 allocation-deallocation checking


From: Bruno Haible
Subject: dirent: Improve GCC 11 allocation-deallocation checking
Date: Sat, 07 Aug 2021 18:07:59 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-210-generic; KDE/5.18.0; x86_64; ; )

2021-08-07  Bruno Haible  <bruno@clisp.org>

        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.

diff --git a/lib/dirent.in.h b/lib/dirent.in.h
index 647a3db..49e0aa9 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/dirent-safer.h b/lib/dirent-safer.h
index e18bb89..70ada06 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/opendirat.h b/lib/opendirat.h
index 845cc55..9765615 100644
--- a/lib/opendirat.h
+++ b/lib/opendirat.h
@@ -17,4 +17,5 @@
 
 #include <dirent.h>
 
-DIR *opendirat (int, char const *, int, int *);
+DIR *opendirat (int, char const *, int, int *)
+  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]