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
std::list<>::remove_if() visits elements multiple times #20894
Comments
Fix erase() invocation arguments in std::list<>::remove() and remove_if()
However, erase() erases from i up to, but not including j, and returns j. So j should be incremented to after the last __pred match (or equals match, in case of remove()) instead:
Similar for remove():
Attached patch makes it so. This also fixes the testcase from the FreeBSD bug for me. |
[Fix erase() invocation arguments in std::list<>::remove()/remove_if(), and std::forward_list<>::remove()https://user-images.githubusercontent.com/9060368/143749821-0559ea8f-c3d7-466b-a4f7-b8d399806da8.gz) |
[Fix erase() invocation arguments in std::list<>::remove()/remove_if(), and std::forward_list<>::remove()https://user-images.githubusercontent.com/60944935/143749820-b4b0221b-5042-40d1-b627-f272d16617b9.gz) |
After the call to erase, we know that either (a) we're at the end of the list, or (b) we're looking at an element that the predicate has returned "false" for. So we can just increment the iterator, and move on.
I don't see that forward_list has this problem, because the code is somewhat different. Committed revision 214736 to fix this (and add tests). |
That's pretty much the same fix I posted on the FreeBSD PR.
I tested that, too and couldn't find problems with forward_list. Thank you for fixing this so fast. |
Ah sorry, I really should have had more coffee before attempting to fix this. :) |
NetBSD llvm branch is also affected. |
Extended Description
When calling std::list<>::remove_if() with a lambda for the condition (I did not test without lambdas), it visits the list entry following a delete twice.
Demo code and a patch can be found with the respective FreeBSD bug report:
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=192303
The text was updated successfully, but these errors were encountered: