From ebf695bc779a63a5730df05ab246305c0ab342e4 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 4 Oct 2019 16:53:56 +0200 Subject: Deprecate calling QList::insert/removeAt with out of bounds index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Users should not call any QList API with indices that are out of bounds. Deprecate this behavior and make sure users get warnings in debug mode and assertions if they disable deprecated functionality. [ChangeLog][Important Behavior Changes] Calling QList::insert() or removeAt() with an out of bounds index is deprecated and will not be supported in Qt 6 anymore. Change-Id: I97adecc2e2aabd36ea2cc69e0895d625f78b32a0 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Frederik Gladhorn --- src/corelib/tools/qlist.cpp | 7 ++++--- src/corelib/tools/qlist.h | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index dfebd57e34..3dc962236c 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -847,9 +847,10 @@ void **QListData::erase(void **xi) /*! \fn template void QList::insert(int i, const T &value) - Inserts \a value at index position \a i in the list. If \a i <= 0, - the value is prepended to the list. If \a i >= size(), the - value is appended to the list. + Inserts \a value at index position \a i in the list. + + If \a i == 0, the value is prepended to the list. If \a i == size(), + the value is appended to the list. Example: \snippet code/src_corelib_tools_qlistdata.cpp 8 diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 425ffa42a5..ffd470efcd 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -580,8 +580,16 @@ inline T &QList::operator[](int i) detach(); return reinterpret_cast(p.at(i))->t(); } template inline void QList::removeAt(int i) -{ if(i >= 0 && i < p.size()) { detach(); - node_destruct(reinterpret_cast(p.at(i))); p.remove(i); } } +{ +#if !QT_DEPRECATED_SINCE(5, 15) + Q_ASSERT_X(i >= 0 && i < p.size(), "QList::removeAt", "index out of range"); +#elif !defined(QT_NO_DEBUG) + if (i < 0 || i >= p.size()) + qWarning("QList::removeAt(): Index out of range."); +#endif + detach(); + node_destruct(reinterpret_cast(p.at(i))); p.remove(i); +} template inline T QList::takeAt(int i) { Q_ASSERT_X(i >= 0 && i < p.size(), "QList::take", "index out of range"); @@ -676,6 +684,12 @@ inline void QList::prepend(const T &t) template inline void QList::insert(int i, const T &t) { +#if !QT_DEPRECATED_SINCE(5, 15) + Q_ASSERT_X(i >= 0 && i <= p.size(), "QList::insert", "index out of range"); +#elif !defined(QT_NO_DEBUG) + if (i < 0 || i > p.size()) + qWarning("QList::insert(): Index out of range."); +#endif if (d->ref.isShared()) { Node *n = detach_helper_grow(i, 1); QT_TRY { -- cgit v1.2.3