diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index 423a539b4e6..98badc80feb 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -78,6 +78,7 @@ private slots: void replaceWithSpecifiedLength(); void replaceWithEmptyNeedleInsertsBeforeEachChar_data(); void replaceWithEmptyNeedleInsertsBeforeEachChar(); + void replaceDoesNotReplaceTheTerminatingNull(); void number(); void number_double_data(); @@ -1608,6 +1609,23 @@ void tst_QByteArray::replaceWithEmptyNeedleInsertsBeforeEachChar() } } +void tst_QByteArray::replaceDoesNotReplaceTheTerminatingNull() +{ + // Try really hard to replace the implicit terminating '\0' byte: + constexpr char content[] = "Hello, World!"; +#define CHECK(...) do { \ + QByteArray ba(content); \ + QCOMPARE(std::as_const(ba).data()[ba.size()], '\0'); \ + ba.replace(__VA_ARGS__); \ + QCOMPARE(ba, content); \ + QCOMPARE(std::as_const(ba).data()[ba.size()], '\0'); \ + } while (false) + CHECK('\0', 'a'); + CHECK(QByteArrayView{"!", 2}, // including \0, matches end of `ba` + QByteArrayView{"!!"}); +#undef CHECK +} + void tst_QByteArray::number() { QCOMPARE(QByteArray::number(quint64(0)), QByteArray("0"));