From b4e7e6bf229664045926633aae90a287e39f6454 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 26 Oct 2016 14:34:34 +0100 Subject: [PATCH] Fix error handling in recursive_directory_iterator::increment * src/filesystem/dir.cc (recursive_directory_iterator::increment): Reset state on error. * testsuite/experimental/filesystem/iterators/ recursive_directory_iterator.cc: Check state after increment error. From-SVN: r241552 --- libstdc++-v3/ChangeLog | 5 +++++ libstdc++-v3/src/filesystem/dir.cc | 5 ++++- .../filesystem/iterators/recursive_directory_iterator.cc | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7d02cf4815f..fadd349bce6 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2016-10-26 Jonathan Wakely + * src/filesystem/dir.cc (recursive_directory_iterator::increment): + Reset state on error. + * testsuite/experimental/filesystem/iterators/ + recursive_directory_iterator.cc: Check state after increment error. + PR libstdc++/78111 * src/filesystem/ops.cc (canonical): Set error for non-existent path. diff --git a/libstdc++-v3/src/filesystem/dir.cc b/libstdc++-v3/src/filesystem/dir.cc index 4640d751127..bcd7dd062ec 100644 --- a/libstdc++-v3/src/filesystem/dir.cc +++ b/libstdc++-v3/src/filesystem/dir.cc @@ -343,7 +343,10 @@ fs::recursive_directory_iterator::increment(error_code& ec) noexcept { _Dir dir = open_dir(top.entry.path(), _M_options, &ec); if (ec) - return *this; + { + _M_dirs.reset(); + return *this; + } if (dir.dirp) _M_dirs->push(std::move(dir)); } diff --git a/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc b/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc index b41c394b41a..79aa178f152 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/iterators/recursive_directory_iterator.cc @@ -81,6 +81,7 @@ test01() VERIFY( iter->path() == p/"d1/d2" ); iter.increment(ec); // should fail to recurse into p/d1/d2 VERIFY( ec ); + VERIFY( iter == fs::recursive_directory_iterator() ); // Test inaccessible sub-directory, skipping permission denied. iter = fs::recursive_directory_iterator(p, opts, ec); -- 2.30.2