QString::assign: Don't crash when passed an empty range

Pick-to: 6.7
Change-Id: I7c02abeb1bd8fa5a8609f163a5a722c2c236fc2b
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit aa2cfb58089dcf46d14606964e30d1c695b64393)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Albert Astals Cid 2024-06-25 00:53:00 +02:00 committed by Qt Cherry-pick Bot
parent a6e3710875
commit 0cbf579ca1
2 changed files with 23 additions and 2 deletions

View File

@ -520,11 +520,13 @@ public:
return *this;
} else if constexpr (QtPrivate::IsCompatibleChar8Type<V>::value) {
assign_helper_char8(first, last);
d.data()[d.size] = u'\0';
if (d.constAllocatedCapacity())
d.data()[d.size] = u'\0';
return *this;
} else {
d.assign(first, last, [](QChar ch) -> char16_t { return ch.unicode(); });
d.data()[d.size] = u'\0';
if (d.constAllocatedCapacity())
d.data()[d.size] = u'\0';
return *this;
}
}

View File

@ -3668,6 +3668,25 @@ void tst_QString::assign()
QCOMPARE(str.size(), 3);
}
// QString &assign(InputIterator, InputIterator)
{
// This needs to be on its own to ensure we call them on empty str
QString str;
const char16_t c16[] = u"٩(⁎❛ᴗ❛⁎)۶ 🤷";
std::u16string c16str(c16);
str.assign(c16str.begin(), c16str.begin());
QCOMPARE(str.size(), 0);
}
{
#ifndef QT_NO_CAST_FROM_ASCII
// This needs to be on its own to ensure we call them on empty str
QString str;
const char c8[] = "a©☻🂤"; // [1, 2, 3, 4] bytes in utf-8 code points
std::string c8str(c8);
str.assign(c8str.begin(), c8str.begin());
QCOMPARE(str.size(), 0);
#endif
}
{
// Forward iterator versions
QString str;