https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97731
Bug ID: 97731 Summary: terminate called in std::experimental::filesystem::recursive_directory_ite rator Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: torsten.hilbrich at secunet dot com Target Milestone: --- Created attachment 49506 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49506&action=edit Reproducer for the problem The recursive_directory_iterator causes a call to terminate when an I/O error is happening on the directory to iterate. It originally occured in an environment where access to a directory was no longer possible and caused EIO reported through a linux device-mapper target. In the test program this behaviour is simulated by replacing the readdir function with a mockup always failing with errno set to EIO. We initially found this problem with libstdc++ as provided with gcc 8.3.0. But I also checked the version from gcc 10.2 and could reproduce the problem. The testing code test.cpp is attached to this ticket. Here is an example session: $ g++ -o test test.cpp -lstdc++fs $ ./test terminate called after throwing an instance of 'std::experimental::filesystem::v1::__cxx11::filesystem_error' what(): filesystem error: directory iterator cannot advance: Input/output error Aborted (core dumped) I already looked at the code and thing I found the culprit. In recursive_directory_iterator(const path&, directory_options options, error_code*) (dir.cc:199) the following method call is found: if (sp->top().advance(ec)) This calls the method "bool advance(bool skip_permission_denied = false)" within _Dir, implicitly converting the std::error_code* to bool. This way, we get roughly the following stack trace: #0 std::filesystem::_Dir_base::advance (this=0x55555559a050, skip_permission_denied=true, ec=...) at dir-common.h:110 #1 0x000055555555eb39 in std::experimental::filesystem::v1::__cxx11::_Dir::advance ( this=0x55555559a050, skip_permission_denied=true, ec=...) at dir.cc:65 #2 0x000055555555ed39 in std::experimental::filesystem::v1::__cxx11::_Dir::advance ( this=0x55555559a050, skip_permission_denied=true) at dir.cc:87 #3 0x000055555555d110 in std::experimental::filesystem::v1::__cxx11::recursive_directory_iterator::recursive_directory_iterator (this=0x7fffffffdd90, p=filesystem::path "/tmp" = {...}, options=std::experimental::filesystem::v1::directory_options::none, ec=0x7fffffffdd60) at dir.cc:199 #4 0x000055555555c07c in std::experimental::filesystem::v1::__cxx11::recursive_directory_iterator::recursive_directory_iterator (this=0x7fffffffdd90, __p=filesystem::path "/tmp" = {...}, __ec=...) at /usr/include/c++/9/experimental/bits/fs_dir.h:280 #5 0x000055555555bb5d in main () at ./test.cpp:17 and finally, an exception is thrown via a method declared noexcept, causing the termination of the program. Torsten