From 8ed2bc919432aaa2f7304b1a6207c056365cc22c Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 15 Jun 2023 02:39:19 +0300 Subject: [PATCH] QByteArray: change append(QByteArray) to match QStringBuilder behavior I.e. concatenating a null byte array and an empty-but-not-null byte array should result in an empty-but-not-null byte array. This matches the behavior of QString::append(QString) too. Fixes: QTBUG-114238 Pick-to: 6.6 Change-Id: Id36d10ee09c08041b7dabda102df48ca6d413d8b Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 11 ++++++++--- .../corelib/text/qbytearray/tst_qbytearray.cpp | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 7f8349131d..42921e99d7 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -2044,9 +2044,14 @@ QByteArray &QByteArray::prepend(const QByteArray &ba) QByteArray &QByteArray::append(const QByteArray &ba) { - if (size() == 0 && ba.size() > d->freeSpaceAtEnd() && ba.d.isMutable()) - return (*this = ba); - return append(QByteArrayView(ba)); + if (!ba.isNull()) { + if (isNull()) { + operator=(ba); + } else if (ba.size()) { + append(QByteArrayView(ba)); + } + } + return *this; } /*! diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index bc84e25651..f5e149836a 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -53,6 +53,7 @@ private slots: void append(); void appendExtended_data(); void appendExtended(); + void appendEmptyNull(); void assign(); void assignShared(); void assignUsesPrependBuffer(); @@ -937,6 +938,23 @@ void tst_QByteArray::appendExtended() QCOMPARE(array.size(), 11); } +void tst_QByteArray::appendEmptyNull() +{ + QByteArray a; + QVERIFY(a.isEmpty()); + QVERIFY(a.isNull()); + + QByteArray b(""); + QVERIFY(b.isEmpty()); + QVERIFY(!b.isNull()); + + // Concatenating a null and an empty-but-not-null byte arrays results in + // an empty but not null byte array + QByteArray r = a + b; + QVERIFY(r.isEmpty()); + QVERIFY(!r.isNull()); +} + void tst_QByteArray::assign() { // QByteArray &assign(QByteArrayView)