Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket #12578: Make directory iterators able to detect when a copy has advanced to the end #37

Closed
wants to merge 2 commits into from

Conversation

mlimber
Copy link

@mlimber mlimber commented Nov 22, 2016

Motivating example is using an iterator (e.g., testing to see if it is at the end) after a copy of it has been advanced to the end:

auto di = fs::directory_iterator( "." );
const auto end = fs::directory_iterator();
const auto dist = std::distance( di, end ); // implicitly iterate a copy of di to the end
                                            // which makes di undetectably invalid.
std::cout << "File count: " << dist << std::endl;
while( di != end ) // crash! Also with: for( const auto& e : di ), etc.
{
  std::cout << *di++ << std::endl;
}

For details, see Ticket #12578.

@mlimber mlimber changed the title Trac #12578: Make directory iterators able to detect when a copy has advanced to the end Ticket #12578: Make directory iterators able to detect when a copy has advanced to the end Nov 22, 2016
@Beman
Copy link
Contributor

Beman commented Nov 23, 2016

I've applied essentially the same fix as your pull request, but with some added comments and a different set of test cases. By the way, the motivating example you give does a post-increment on an iterator than will be the end iterator the last time through the loop.

Thanks for the bug report!

--Beman

@Beman Beman closed this Nov 23, 2016
@mlimber
Copy link
Author

mlimber commented Nov 23, 2016

Fixed the increment. Thanks for processing it so quickly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants