From c1c15abc8d0d4f0804dba49e95ffdde5f8591413 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 3 Nov 2021 16:04:31 +0100 Subject: QByteArrayList: fix narrowing in join() implementations [1/2] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We forgot to adjust the interface and implementation of join() to the int → qsizetype change in Qt 6. This part of the two-part patch fixes things in a forwards-BC way, to allow picking into released versions. The forwards-BC break is in the second patch of the series. [ChangeLog][QtCore][QByteArrayList] Fixed a bug when calling join() on lists with more than INTMAX elements. Pick-to: 6.2 Change-Id: I26976864e77169ff0db7c672d1d42d88dbfcc437 Reviewed-by: Edward Welbourne Reviewed-by: Mårten Nordheim --- src/corelib/text/qbytearraylist.cpp | 14 +++++++------- src/corelib/text/qbytearraylist.h | 3 +++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/corelib/text/qbytearraylist.cpp b/src/corelib/text/qbytearraylist.cpp index 5142869c72..44a0f6be95 100644 --- a/src/corelib/text/qbytearraylist.cpp +++ b/src/corelib/text/qbytearraylist.cpp @@ -130,12 +130,12 @@ QT_BEGIN_NAMESPACE element separated by the given \a separator. */ -static int QByteArrayList_joinedSize(const QByteArrayList *that, int seplen) +static qsizetype QByteArrayList_joinedSize(const QByteArrayList *that, qsizetype seplen) { - int totalLength = 0; - const int size = that->size(); + qsizetype totalLength = 0; + const qsizetype size = that->size(); - for (int i = 0; i < size; ++i) + for (qsizetype i = 0; i < size; ++i) totalLength += that->at(i).size(); if (size > 0) @@ -147,10 +147,10 @@ static int QByteArrayList_joinedSize(const QByteArrayList *that, int seplen) QByteArray QtPrivate::QByteArrayList_join(const QByteArrayList *that, const char *sep, int seplen) { QByteArray res; - if (const int joinedSize = QByteArrayList_joinedSize(that, seplen)) + if (const qsizetype joinedSize = QByteArrayList_joinedSize(that, seplen)) res.reserve(joinedSize); // don't call reserve(0) - it allocates one byte for the NUL - const int size = that->size(); - for (int i = 0; i < size; ++i) { + const qsizetype size = that->size(); + for (qsizetype i = 0; i < size; ++i) { if (i) res.append(sep, seplen); res += that->at(i); diff --git a/src/corelib/text/qbytearraylist.h b/src/corelib/text/qbytearraylist.h index 7e8fe89bab..83e43e817d 100644 --- a/src/corelib/text/qbytearraylist.h +++ b/src/corelib/text/qbytearraylist.h @@ -46,6 +46,8 @@ #include +#include + QT_BEGIN_NAMESPACE #if !defined(QT_NO_JAVA_STYLE_ITERATORS) @@ -79,6 +81,7 @@ public: { return QtPrivate::QByteArrayList_join(self(), nullptr, 0); } inline QByteArray join(QByteArrayView sep) const // ### Qt 7: merge with the () overload { + Q_ASSERT(sep.size() <= (std::numeric_limits::max)()); return QtPrivate::QByteArrayList_join(self(), sep.data(), sep.size()); } Q_WEAK_OVERLOAD -- cgit v1.2.3