summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2021-11-03 16:04:31 +0100
committerMarc Mutz <marc.mutz@qt.io>2021-11-04 16:11:35 +0000
commitc1c15abc8d0d4f0804dba49e95ffdde5f8591413 (patch)
treeec3dd6185b4cd856f227019ab2d6e6dd1d0ed958
parent5d19219eebeceb41f6ddab1052da0117a663534a (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. Pick-to: 6.2 Change-Id: I26976864e77169ff0db7c672d1d42d88dbfcc437 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
-rw-r--r--src/corelib/text/qbytearraylist.cpp14
-rw-r--r--src/corelib/text/qbytearraylist.h3
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 <QtCore/qbytearray.h>
+#include <limits>
+
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<int>::max)());
return QtPrivate::QByteArrayList_join(self(), sep.data(), sep.size());
}
Q_WEAK_OVERLOAD