-
Notifications
You must be signed in to change notification settings - Fork 67
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
Past The End iterator dereference in filter_iterator #71
Comments
You create the end iterator incorrectly. You need to pass the base end iterator twice. |
@grisumbras Good spot. Confirmed clean: https://godbolt.org/z/65TYrMe44 I think that the defaulted value is questionable - not PitOfSuccess design.
If it's a sentinel anyways, there might be better ways of constructing the iterator. Right now // c++11
auto first = boost::make_filter_iterator(False{}, begin(s), end(s)),
last = boost::make_filter_iterator(first.predicate(), first.end(), first.end());
for (; first != last; ++first) {
std::cout << static_cast<int>(*first) << " ";
}; Arguably a c++03(!) version not using // c++03
boost::filter_iterator<False, char const*> //
first(False(), s.begin(), s.end()), //
last(first.end(), first.end());
for (; first != last; ++first) {
std::cout << static_cast<int>(*first) << " ";
}; And of course c++17 allows: // c++17
boost::filter_iterator //
first(False(), s.begin(), s.end()), //
last(first.predicate(), first.end(), first.end());
for (; first != last; ++first) {
std::cout << static_cast<int>(*first) << " ";
}; The common problem is that the // boost range
using boost::adaptors::filtered;
for (auto ch : s | filtered(False{})) {
std::cout << static_cast<int>(ch) << " ";
}; Compare all four versions: https://godbolt.org/z/dqcdEv5cE TL;DRI'd say in the light of the above, the defaulted end-iterator value doesn't pull its weight and should not be defaulted. |
Motivating example that's extremely easy to use wrong (and really should not compile IMO): https://stackoverflow.com/a/71933410/85371 |
This simple test program segfaults on my GCC box with Boost 1.78
See it live with ASAN: https://godbolt.org/z/8c9f1drGn
The text was updated successfully, but these errors were encountered: