From 3f0950f65abb5162c42b51a9bb32c9e87deeb405 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 27 Apr 2023 01:42:25 +0200 Subject: fdopendir: Fix fd leak and test failure on native Windows. * lib/dirent-private.h: On mingw, define 'struct gl_directory' as a wrapper around the original DIR. On MSVC, add an 'fd_to_close' field to 'struct gl_directory'. * lib/dirent.in.h (DIR): Define when DIR_HAS_FD_MEMBER is 0, i.e. on both mingw and MSVC. (GNULIB_defined_DIR): New macro. (opendir): Avoid incompatible redeclaration. (readdir): Consider REPLACE_READDIR. (rewinddir): Consider REPLACE_REWINDDIR. * m4/dirent_h.m4 (gl_DIRENT_DIR): New macro. (gl_DIRENT_H): Invoke it. (gl_DIRENT_H_DEFAULTS): Initialize REPLACE_READDIR, REPLACE_REWINDDIR. * modules/dirent (Makefile.am): Substitute DIR_HAS_FD_MEMBER, REPLACE_READDIR, REPLACE_REWINDDIR. -- * lib/dirfd.c (dirfd): If GNULIB_defined_DIR, just use the 'fd_to_close' field. * m4/dirfd.m4 (gl_FUNC_DIRFD): Set HAVE_DIRFD. Don't set REPLACE_DIRFD to 1 if HAVE_DIRFD is 0. If DIR_HAS_FD_MEMBER is 0, ensure dirfd.c gets compiled. * modules/dirfd (Files): Add lib/dirent-private.h. (Depends-on, configure.ac): Simplify conditions. -- * lib/closedir.c: Include always, for free(). (closedir): If GNULIB_defined_DIR, arrange to call close(dirfd(dirp)) at the end. On mingw, call free() of dirp. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/closedir.m4 (gl_FUNC_CLOSEDIR): Don't set REPLACE_CLOSEDIR to 1 if HAVE_CLOSEDIR is 0. If DIR_HAS_FD_MEMBER is 0, ensure closedir.c gets compiled. -- * lib/opendir.c: Include always. Include . (opendir): On mingw, allocate the 'struct gl_directory' through malloc. If GNULIB_defined_DIR, set the 'fd_to_close' field to -1. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/opendir.m4 (gl_FUNC_OPENDIR): Don't set REPLACE_OPENDIR to 1 if HAVE_OPENDIR is 0. If DIR_HAS_FD_MEMBER is 0, ensure opendir.c gets compiled. -- * lib/fdopendir.c (fdopendir): If GNULIB_defined_DIR, use a simple implementation based on opendir and the fchdir module. If __KLIBC__, don't define unused auxiliary functions. * modules/fdopendir (Files): Add lib/dirent-private.h. -- * lib/readdir.c (readdir): On mingw, redirect to the original readdir function. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/readdir.m4 (gl_FUNC_READDIR): If DIR_HAS_FD_MEMBER is 0, ensure readdir.c gets compiled. * modules/readdir (configure.ac): Consider REPLACE_READDIR. -- * lib/rewinddir.c (rewinddir): On mingw, redirect to the original rewinddir function. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/rewinddir.m4 (gl_FUNC_REWINDDIR): If DIR_HAS_FD_MEMBER is 0, ensure rewinddir.c gets compiled. * modules/rewinddir (configure.ac): Consider REPLACE_REWINDDIR. -- * lib/fchdir.c (dir_info_t): Remove a FIXME. --- ChangeLog | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'ChangeLog') diff --git a/ChangeLog b/ChangeLog index 7a377b243f..212d611c69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,66 @@ +2023-04-26 Bruno Haible + + fdopendir: Fix fd leak and test failure on native Windows. + -- + * lib/dirent-private.h: On mingw, define 'struct gl_directory' as a + wrapper around the original DIR. On MSVC, add an 'fd_to_close' field to + 'struct gl_directory'. + * lib/dirent.in.h (DIR): Define when DIR_HAS_FD_MEMBER is 0, i.e. on + both mingw and MSVC. + (GNULIB_defined_DIR): New macro. + (opendir): Avoid incompatible redeclaration. + (readdir): Consider REPLACE_READDIR. + (rewinddir): Consider REPLACE_REWINDDIR. + * m4/dirent_h.m4 (gl_DIRENT_DIR): New macro. + (gl_DIRENT_H): Invoke it. + (gl_DIRENT_H_DEFAULTS): Initialize REPLACE_READDIR, REPLACE_REWINDDIR. + * modules/dirent (Makefile.am): Substitute DIR_HAS_FD_MEMBER, + REPLACE_READDIR, REPLACE_REWINDDIR. + -- + * lib/dirfd.c (dirfd): If GNULIB_defined_DIR, just use the + 'fd_to_close' field. + * m4/dirfd.m4 (gl_FUNC_DIRFD): Set HAVE_DIRFD. Don't set REPLACE_DIRFD + to 1 if HAVE_DIRFD is 0. If DIR_HAS_FD_MEMBER is 0, ensure dirfd.c gets + compiled. + * modules/dirfd (Files): Add lib/dirent-private.h. + (Depends-on, configure.ac): Simplify conditions. + -- + * lib/closedir.c: Include always, for free(). + (closedir): If GNULIB_defined_DIR, arrange to call close(dirfd(dirp)) at + the end. On mingw, call free() of dirp. Prefer testing HAVE_DIRENT_H, + for consistency with dirent.h. + * m4/closedir.m4 (gl_FUNC_CLOSEDIR): Don't set REPLACE_CLOSEDIR to 1 if + HAVE_CLOSEDIR is 0. If DIR_HAS_FD_MEMBER is 0, ensure closedir.c gets + compiled. + -- + * lib/opendir.c: Include always. Include . + (opendir): On mingw, allocate the 'struct gl_directory' through malloc. + If GNULIB_defined_DIR, set the 'fd_to_close' field to -1. Prefer + testing HAVE_DIRENT_H, for consistency with dirent.h. + * m4/opendir.m4 (gl_FUNC_OPENDIR): Don't set REPLACE_OPENDIR to 1 if + HAVE_OPENDIR is 0. If DIR_HAS_FD_MEMBER is 0, ensure opendir.c gets + compiled. + -- + * lib/fdopendir.c (fdopendir): If GNULIB_defined_DIR, use a simple + implementation based on opendir and the fchdir module. If __KLIBC__, + don't define unused auxiliary functions. + * modules/fdopendir (Files): Add lib/dirent-private.h. + -- + * lib/readdir.c (readdir): On mingw, redirect to the original readdir + function. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. + * m4/readdir.m4 (gl_FUNC_READDIR): If DIR_HAS_FD_MEMBER is 0, ensure + readdir.c gets compiled. + * modules/readdir (configure.ac): Consider REPLACE_READDIR. + -- + * lib/rewinddir.c (rewinddir): On mingw, redirect to the original + rewinddir function. Prefer testing HAVE_DIRENT_H, for consistency with + dirent.h. + * m4/rewinddir.m4 (gl_FUNC_REWINDDIR): If DIR_HAS_FD_MEMBER is 0, ensure + rewinddir.c gets compiled. + * modules/rewinddir (configure.ac): Consider REPLACE_REWINDDIR. + -- + * lib/fchdir.c (dir_info_t): Remove a FIXME. + 2023-04-26 Bruno Haible fchdir tests: Fix test failure on native Windows. -- cgit v1.2.1