diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index e2cb7fbf23..b75c2975b6 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -581,14 +581,15 @@ typename QVector::iterator QVector::insert(iterator before, size_type n, c template typename QVector::iterator QVector::erase(iterator abegin, iterator aend) { - if (abegin < d->begin()) - abegin = d->begin(); - if (aend > d->end()) - aend = d->end(); + const int itemsToErase = aend - abegin; + if (!itemsToErase) + return abegin; + + Q_ASSERT(abegin >= d->begin()); + Q_ASSERT(aend <= d->end()); Q_ASSERT(abegin <= aend); - const int itemsToErase = aend - abegin; const int itemsUntouched = abegin - d->begin(); // FIXME we could do a proper realloc, which copy constructs only needed data. diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 56570b8e53..09d3051ca5 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -796,16 +796,12 @@ void tst_QVector::eraseEmpty() const { { QVector v; - v.erase(v.begin()); - QCOMPARE(v.size(), 0); v.erase(v.begin(), v.end()); QCOMPARE(v.size(), 0); } { QVector v; v.setSharable(false); - v.erase(v.begin()); - QCOMPARE(v.size(), 0); v.erase(v.begin(), v.end()); QCOMPARE(v.size(), 0); } @@ -836,8 +832,6 @@ void tst_QVector::eraseEmptyReserved() const { QVector v; v.reserve(10); - v.erase(v.begin()); - QCOMPARE(v.size(), 0); v.erase(v.begin(), v.end()); QCOMPARE(v.size(), 0); } @@ -845,8 +839,6 @@ void tst_QVector::eraseEmptyReserved() const QVector v; v.reserve(10); v.setSharable(false); - v.erase(v.begin()); - QCOMPARE(v.size(), 0); v.erase(v.begin(), v.end()); QCOMPARE(v.size(), 0); }