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>
This commit is contained in:
Marc Mutz 2021-11-03 16:04:31 +01:00
parent 5d19219eeb
commit c1c15abc8d
2 changed files with 10 additions and 7 deletions

View File

@ -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);

View File

@ -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