Verified Commit ad8f0094 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

AkRanges: mark FilterIterator as an InputIterator

Normally the AkRanges' BaseIterator inherits the iterator category from the
source iterator, which is RandomAccessIterator for most Qt containers.
In case of our FilterIterator, however, we reduce it to a simple
InputIterator. Not only because we don't implement correctly all of the
required RandomAccessIterator semantics (which we could do), but it
would actually have negative performance impact with STL algorithms like
std::find(), since some of the operations would be costly to perform on
the FilterIterator (e.g. last-first would require to loop over the
entire container).

This was actually discovered by ASAN detecting heap-buffer-overflow in
AkRangesTest::testNone() as the loop tried to dereference past-the-end
parent 31578c12
......@@ -184,6 +184,10 @@ template<typename RangeLike, typename Predicate, typename Iterator = typename Ra
class FilterIterator : public IteratorBase<FilterIterator<RangeLike, Predicate>, RangeLike>
// Filter iterator is just an InputIterator (for complexity reasons).
// It actually makes it more efficient with STL algos like std::find()
using iterator_category = std::input_iterator_tag;
FilterIterator(const Iterator &iter, const Iterator &end, const Predicate &predicate, const RangeLike &range)
: IteratorBase<FilterIterator, RangeLike>(iter, range)
, mPredicate(predicate)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment