From 8e2c6a590429892d9fecf9a0ccc7c6a00d6c7b6d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 13 Feb 2025 21:35:19 +0100 Subject: [PATCH] tst_QByteArray: check replace() doesn't replace the terminating \0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It doesn't. Pick-to: 6.8 6.5 5.15 Change-Id: Ica3fb2b8a65d0f9d14490ecdcce72eba82258916 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Ahmad Samir (cherry picked from commit fff69288781bf946ad645fd781ad51ab48ebcbdc) --- .../corelib/text/qbytearray/tst_qbytearray.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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"));