QByteArray: fix append() with a negative length

The documentation says that if it's negative, we find the null
termination. This bug was introduced with the clean up to use
QByteArrayView in commit 8897aa071a.

Fixes: QTBUG-99640
Pick-to: 6.3 6.2
Change-Id: Ib42b3adc93bf4d43bd55fffd16c89fa4a960f3a9
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Thiago Macieira 2022-01-09 06:13:13 -08:00
parent 17dca04a61
commit bb35b2f64d
2 changed files with 3 additions and 2 deletions

View File

@ -249,9 +249,9 @@ public:
QByteArray &append(char c);
inline QByteArray &append(qsizetype count, char c);
QByteArray &append(const char *s)
{ return append(QByteArrayView(s, qsizetype(qstrlen(s)))); }
{ return append(s, -1); }
QByteArray &append(const char *s, qsizetype len)
{ return append(QByteArrayView(s, len)); }
{ return append(QByteArrayView(s, len < 0 ? qsizetype(qstrlen(s)) : len)); }
QByteArray &append(const QByteArray &a);
QByteArray &append(QByteArrayView a)
{ return insert(size(), a); }

View File

@ -957,6 +957,7 @@ void tst_QByteArray::append()
QCOMPARE(QByteArray().append(2, 'a'), QByteArray("aa"));
QCOMPARE(QByteArray().append(QByteArray("data")), QByteArray("data"));
QCOMPARE(QByteArray().append(data), QByteArray("data"));
QCOMPARE(QByteArray().append(data, -1), QByteArray("data"));
QCOMPARE(QByteArray().append(data, 2), QByteArray("da"));
QCOMPARE(QByteArray().append(QByteArrayView(data)), QByteArray("data"));