From ac4c0e1f0293c6d533c0537b3e0139733e4212eb Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 7 Dec 2021 14:16:31 +0100 Subject: [PATCH] QVarLengthArray: don't mix iterator/const_iterator in offset calc The functions aren't const so begin() will return a mutable iterator. The function arguments, however, are const_iterators. It doesn't matter for QVLA, which isn't implicitly shared, but code hygiene suggests to use cbegin() instead, to compare const_iterators to const_iterators. Change-Id: I9bfa993780ee4b68d13f6b6410772b0f1ccedad3 Reviewed-by: Fabian Kosmale --- src/corelib/tools/qvarlengtharray.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index a975aa4b0b6..a6e41168a83 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -631,7 +631,7 @@ Q_OUTOFLINE_TEMPLATE auto QVarLengthArray::emplace(const_iterator b Q_ASSERT(size() <= capacity()); Q_ASSERT(capacity() > 0); - qsizetype offset = qsizetype(before - begin()); + qsizetype offset = qsizetype(before - cbegin()); if (size() == capacity()) reserve(size() * 2); if constexpr (!QTypeInfo::isRelocatable) { @@ -661,7 +661,7 @@ Q_OUTOFLINE_TEMPLATE typename QVarLengthArray::iterator QVarLengthA { Q_ASSERT_X(isValidIterator(before), "QVarLengthArray::insert", "The specified const_iterator argument 'before' is invalid"); - qsizetype offset = qsizetype(before - begin()); + qsizetype offset = qsizetype(before - cbegin()); if (n != 0) { const T copy(t); // `t` could alias an element in [begin(), end()[ resize(size() + n); @@ -691,8 +691,8 @@ Q_OUTOFLINE_TEMPLATE typename QVarLengthArray::iterator QVarLengthA Q_ASSERT_X(isValidIterator(abegin), "QVarLengthArray::insert", "The specified const_iterator argument 'abegin' is invalid"); Q_ASSERT_X(isValidIterator(aend), "QVarLengthArray::insert", "The specified const_iterator argument 'aend' is invalid"); - qsizetype f = qsizetype(abegin - begin()); - qsizetype l = qsizetype(aend - begin()); + qsizetype f = qsizetype(abegin - cbegin()); + qsizetype l = qsizetype(aend - cbegin()); qsizetype n = l - f; if constexpr (QTypeInfo::isComplex) {