From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 25CDD3860C33; Mon, 9 Nov 2020 14:53:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25CDD3860C33 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r9-9034] libstdc++: Use non-throwing increment in recursive_directory_iterator [PR 97731] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: 97dd08aa244af1e236dec54c014a3e03d34594d0 X-Git-Newrev: c9769a6eee38c396b797cffd819957fa9f1926b1 Message-Id: <20201109145356.25CDD3860C33@sourceware.org> Date: Mon, 9 Nov 2020 14:53:56 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 Nov 2020 14:53:56 -0000 https://gcc.gnu.org/g:c9769a6eee38c396b797cffd819957fa9f1926b1 commit r9-9034-gc9769a6eee38c396b797cffd819957fa9f1926b1 Author: Jonathan Wakely Date: Thu Nov 5 17:26:13 2020 +0000 libstdc++: Use non-throwing increment in recursive_directory_iterator [PR 97731] As described in the PR, the recursive_directory_iterator constructor calls advance(ec), but ec is a pointer so it calls _Dir::advance(bool). The intention was to either call advance() or advance(*ec) depending whether the pointer is null or not. This fixes the bug and renames the parameter to ecptr to make similar mistakes less likely in future. libstdc++-v3/ChangeLog: PR libstdc++/97731 * src/filesystem/dir.cc (recursive_directory_iterator): Call the right overload of _Dir::advance. * testsuite/experimental/filesystem/iterators/97731.cc: New test. (cherry picked from commit 2f93a2a03a343a29f614a530d7657f1ed6347ed5) Diff: --- libstdc++-v3/src/filesystem/dir.cc | 18 ++++---- .../experimental/filesystem/iterators/97731.cc | 49 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/src/filesystem/dir.cc b/libstdc++-v3/src/filesystem/dir.cc index ac3bd3aafb3..d7d8eb74066 100644 --- a/libstdc++-v3/src/filesystem/dir.cc +++ b/libstdc++-v3/src/filesystem/dir.cc @@ -187,16 +187,16 @@ struct fs::recursive_directory_iterator::_Dir_stack : std::stack<_Dir> fs::recursive_directory_iterator:: recursive_directory_iterator(const path& p, directory_options options, - error_code* ec) + error_code* ecptr) : _M_options(options), _M_pending(true) { - if (ec) - ec->clear(); if (posix::DIR* dirp = posix::opendir(p.c_str())) { + if (ecptr) + ecptr->clear(); auto sp = std::make_shared<_Dir_stack>(); sp->push(_Dir{ dirp, p }); - if (sp->top().advance(ec)) + if (ecptr ? sp->top().advance(*ecptr) : sp->top().advance()) _M_dirs.swap(sp); } else @@ -204,14 +204,18 @@ recursive_directory_iterator(const path& p, directory_options options, const int err = errno; if (err == EACCES && is_set(options, fs::directory_options::skip_permission_denied)) - return; + { + if (ecptr) + ecptr->clear(); + return; + } - if (!ec) + if (!ecptr) _GLIBCXX_THROW_OR_ABORT(filesystem_error( "recursive directory iterator cannot open directory", p, std::error_code(err, std::generic_category()))); - ec->assign(err, std::generic_category()); + ecptr->assign(err, std::generic_category()); } } diff --git a/libstdc++-v3/testsuite/experimental/filesystem/iterators/97731.cc b/libstdc++-v3/testsuite/experimental/filesystem/iterators/97731.cc new file mode 100644 index 00000000000..c6a9d5663fe --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/filesystem/iterators/97731.cc @@ -0,0 +1,49 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" } +// { dg-do run { target c++11 } } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +bool used_custom_readdir = false; + +extern "C" void* readdir(void*) +{ + used_custom_readdir = true; + errno = EIO; + return nullptr; +} + +void +test01() +{ + using std::experimental::filesystem::recursive_directory_iterator; + std::error_code ec; + recursive_directory_iterator it(".", ec); + if (used_custom_readdir) + VERIFY( ec.value() == EIO ); +} + +int +main() +{ + test01(); +}