From d24aa28b4c3841ecd35401c7c3064e68f7ec7589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Tue, 5 Mar 2013 16:23:20 +0100 Subject: QMap - check if iterator arguments are valid (in debugmode) This patch adds a debug-tests in erase, insert (with hint) and insertMulti (with hint) that ensures the iterator-argument is valid (and e.g not from another map) Change-Id: I7920131bc9712543183cabf13c7603bd0e12880d Reviewed-by: Thiago Macieira --- src/corelib/tools/qmap.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src') diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 449fcbca0a..8c15afd9f8 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -557,6 +557,18 @@ public: private: void detach_helper(); + bool isValidIterator(const const_iterator &ci) const + { +#if defined(QT_DEBUG) && !defined(Q_MAP_NO_ITERATOR_DEBUG) + const QMapNodeBase *n = ci.i; + while (n->parent()) + n = n->parent(); + return n->left == d->root(); +#else + Q_UNUSED(ci); + return true; +#endif + } }; template @@ -670,6 +682,8 @@ typename QMap::iterator QMap::insert(const_iterator pos, const K if (d->ref.isShared()) return this->insert(akey, avalue); + Q_ASSERT_X(isValidIterator(pos), "QMap::insert", "The specified const_iterator argument 'it' is invalid"); + if (pos == constEnd()) { // Hint is that the Node is larger than (or equal to) the largest value. Node *n = static_cast(pos.i->left); @@ -753,6 +767,8 @@ typename QMap::iterator QMap::insertMulti(const_iterator pos, co if (d->ref.isShared()) return this->insertMulti(akey, avalue); + Q_ASSERT_X(isValidIterator(pos), "QMap::insertMulti", "The specified const_iterator argument 'pos' is invalid"); + if (pos == constEnd()) { // Hint is that the Node is larger than (or equal to) the largest value. Node *n = static_cast(pos.i->left); @@ -895,6 +911,8 @@ Q_OUTOFLINE_TEMPLATE typename QMap::iterator QMap::erase(iterato if (it == iterator(d->end())) return it; + Q_ASSERT_X(isValidIterator(const_iterator(it)), "QMap::erase", "The specified iterator argument 'it' is invalid"); + Node *n = it.i; ++it; d->deleteNode(n); -- cgit v1.2.3