Skip to content

Commit d61805a

Browse files
var-constldionne
authored andcommitted
[libc++] Fix double file closing in std::filesystem::remove_all().
According to Linux documentation (see e.g. https://linux.die.net/man/3/closedir): > A successful call to `closedir()` also closes the underlying file > descriptor associated with `dirp`. Thus, calling `close()` after a successful call to `closedir()` is at best redundant. Worse, should a different thread open a file in-between the calls to `closedir()` and `close()` and get the same file descriptor, the call to `close()` might actually close a different file than was intended. rdar://89251874 Differential Revision: https://reviews.llvm.org/D120453 (cherry picked from commit 3906ebf)
1 parent f8ca5fa commit d61805a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

libcxx/src/filesystem/operations.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,12 +1414,14 @@ uintmax_t remove_all_impl(int parent_directory, const path& p, error_code& ec) {
14141414
if (fd != -1) {
14151415
// If that worked, iterate over the contents of the directory and
14161416
// remove everything in it, recursively.
1417-
scope_exit close_fd([=] { ::close(fd); });
14181417
DIR* stream = ::fdopendir(fd);
14191418
if (stream == nullptr) {
1419+
::close(fd);
14201420
ec = detail::capture_errno();
14211421
return 0;
14221422
}
1423+
// Note: `::closedir` will also close the associated file descriptor, so
1424+
// there should be no call to `close(fd)`.
14231425
scope_exit close_stream([=] { ::closedir(stream); });
14241426

14251427
uintmax_t count = 0;

0 commit comments

Comments
 (0)