diff options
author | Thorbjørn Martsum <tmartsum@gmail.com> | 2013-03-05 16:23:20 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-07-12 16:15:44 +0200 |
commit | d24aa28b4c3841ecd35401c7c3064e68f7ec7589 (patch) | |
tree | bf66af44063f07b9d2515dd7b01ad1027425ed29 /src/corelib | |
parent | 1866c13b7dd48aa0c6ede1cf7907a2640e9399f8 (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qmap.h | 18 |
1 files changed, 18 insertions, 0 deletions
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 <class Key, class T> @@ -670,6 +682,8 @@ typename QMap<Key, T>::iterator QMap<Key, T>::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<Node *>(pos.i->left); @@ -753,6 +767,8 @@ typename QMap<Key, T>::iterator QMap<Key, T>::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<Node *>(pos.i->left); @@ -895,6 +911,8 @@ Q_OUTOFLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::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); |