diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 5902b015167..bf3327b9f95 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -308,7 +308,8 @@ public: QByteArray &assign(InputIterator first, InputIterator last) { d.assign(first, last); - d.data()[d.size] = '\0'; + if (d.data()) + d.data()[d.size] = '\0'; return *this; } diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index 81d79da38b3..fea603faed7 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -1017,6 +1017,11 @@ void tst_QByteArray::assign() QByteArrayView test; QList l = {'\0', 'T', 'E', 'S', 'T'}; + + ba.assign(l.begin(), l.begin()); + QVERIFY(ba.isEmpty()); + QCOMPARE(*ba.constData(), '\0'); + ba.assign(l.begin(), l.end()); test = "\0TEST"_ba; QCOMPARE(ba, test); @@ -1033,6 +1038,10 @@ void tst_QByteArray::assign() test = "T\0ST"_ba; QCOMPARE(ba, test); QCOMPARE(ba.size(), test.size()); + + ba.assign(l.begin(), l.begin()); + QVERIFY(ba.isEmpty()); + QCOMPARE(*ba.constData(), '\0'); } // Test chaining {