diff options
author | Marc Mutz <marc.mutz@qt.io> | 2021-11-03 16:04:31 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2021-11-05 11:35:02 +0100 |
commit | 069dfef5e8998de408b1ef4d5c94dbb4d3648322 (patch) | |
tree | de99ae5817e57ae2d7da2de9c53c65f7ed6577ef /src | |
parent | 42f157d0826c31baa80b81410f44215c03a38e88 (diff) |
QByteArrayList: fix narrowing in join() implementations [1/2]
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.
Change-Id: I26976864e77169ff0db7c672d1d42d88dbfcc437
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit c1c15abc8d0d4f0804dba49e95ffdde5f8591413)
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qbytearraylist.cpp | 14 | ||||
-rw-r--r-- | src/corelib/text/qbytearraylist.h | 7 |
2 files changed, 13 insertions, 8 deletions
diff --git a/src/corelib/text/qbytearraylist.cpp b/src/corelib/text/qbytearraylist.cpp index c815e766ab..4a1df30216 100644 --- a/src/corelib/text/qbytearraylist.cpp +++ b/src/corelib/text/qbytearraylist.cpp @@ -122,12 +122,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) @@ -139,10 +139,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 e00586291f..677161c21c 100644 --- a/src/corelib/text/qbytearraylist.h +++ b/src/corelib/text/qbytearraylist.h @@ -46,6 +46,8 @@ #include <QtCore/qbytearray.h> +#include <limits> + QT_BEGIN_NAMESPACE #if !defined(QT_NO_JAVA_STYLE_ITERATORS) @@ -78,7 +80,10 @@ public: inline QByteArray join() const { return QtPrivate::QByteArrayList_join(self(), nullptr, 0); } inline QByteArray join(const QByteArray &sep) const - { return QtPrivate::QByteArrayList_join(self(), sep.constData(), sep.size()); } + { + Q_ASSERT(sep.size() <= (std::numeric_limits<int>::max)()); + return QtPrivate::QByteArrayList_join(self(), sep.data(), sep.size()); + } inline QByteArray join(char sep) const { return QtPrivate::QByteArrayList_join(self(), &sep, 1); } }; |