diff options
author | Thorbjørn Martsum <tmartsum@gmail.com> | 2013-05-02 19:33:37 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-07-12 16:15:44 +0200 |
commit | ca3d8411b29bb4cede1af9b2e871b1ba3dc6f4cf (patch) | |
tree | 0609810bcff5b4a160f9e388e72db8f98cb854a3 /src | |
parent | 578fc87dffafd4211d7223bfbc3b1de086ccc45d (diff) |
QList - check if iterators arguments are valid (in debugmode)
This adds a check of the iterator values in erase and insert.
Change-Id: I78403dcbd24fd7c6beb86d3c827a8e233963e770
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qlist.h | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 0592c24e9f..8a9d364eac 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -346,6 +346,11 @@ private: void node_destruct(Node *n); void node_copy(Node *from, Node *to, Node *src); void node_destruct(Node *from, Node *to); + + bool isValidIterator(const iterator &i) const + { + return (constBegin().i <= i.i) && (i.i <= constEnd().i); + } }; #if defined(Q_CC_BOR) @@ -433,6 +438,8 @@ Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l) template <typename T> inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t) { + Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid"); + int iBefore = int(before.i - reinterpret_cast<Node *>(p.begin())); Node *n = reinterpret_cast<Node *>(p.insert(iBefore)); QT_TRY { @@ -445,8 +452,11 @@ inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t) } template <typename T> inline typename QList<T>::iterator QList<T>::erase(iterator it) -{ node_destruct(it.i); - return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); } +{ + Q_ASSERT_X(isValidIterator(it), "QList::erase", "The specified iterator argument 'it' is invalid"); + node_destruct(it.i); + return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); +} template <typename T> inline const T &QList<T>::at(int i) const { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range"); @@ -811,6 +821,9 @@ template <typename T> Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst, typename QList<T>::iterator alast) { + Q_ASSERT_X(isValidIterator(afirst), "QList::erase", "The specified iterator argument 'afirst' is invalid"); + Q_ASSERT_X(isValidIterator(alast), "QList::erase", "The specified iterator argument 'alast' is invalid"); + for (Node *n = afirst.i; n < alast.i; ++n) node_destruct(n); int idx = afirst - begin(); |