diff options
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 22 | ||||
-rw-r--r-- | src/corelib/text/qstring.cpp | 10 | ||||
-rw-r--r-- | tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp | 4 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstring/tst_qstring.cpp | 4 |
4 files changed, 13 insertions, 27 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index f8c2759507..b85a46b146 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1714,23 +1714,11 @@ void QByteArray::resize(int size) if (size < 0) size = 0; - if (!d->isShared() && !d->isMutable() && size < int(d.size)) { - d.size = size; - return; - } - - if (size == 0 && !(d->flags() & Data::CapacityReserved)) { - d = DataPointer(Data::allocate(0), 0); - } else { - if (d->needsDetach() || size > capacity() - || (!(d->flags() & Data::CapacityReserved) && size < int(d.size) - && size < (capacity() >> 1))) - reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward); - d.size = size; - if (d->isMutable()) { - d.data()[size] = '\0'; - } - } + if (d->needsDetach() || size > capacity()) + reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward); + d.size = size; + if (d->allocatedCapacity()) + d.data()[size] = 0; } /*! diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 57be0eabcf..12b04b8ba7 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2242,17 +2242,11 @@ void QString::resize(int size) if (size < 0) size = 0; - if (!d->isShared() && !d->isMutable() && size < int(d.size)) { - d.size = size; - return; - } - if (d->needsDetach() || size > capacity()) reallocData(uint(size) + 1u, true); d.size = size; - if (d->isMutable()) { - d.data()[size] = '\0'; - } + if (d->allocatedCapacity()) + d.data()[size] = 0; } /*! diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index 67259c04dc..e90f4ff14e 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -1606,7 +1606,9 @@ void tst_QByteArray::resizeAfterFromRawData() QByteArray array = QByteArray::fromRawData(buffer.constData(), buffer.size()); QVERIFY(array.constData() == buffer.constData()); array.resize(5); - QVERIFY(array.constData() == buffer.constData()); + QVERIFY(array.constData() != buffer.constData()); + // check null termination + QVERIFY(array.constData()[5] == 0); } void tst_QByteArray::appendAfterFromRawData() diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 38df331cc2..ef93c732ac 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -6238,7 +6238,9 @@ void tst_QString::resizeAfterFromRawData() QString array = QString::fromRawData(buffer.constData(), buffer.size()); QVERIFY(array.constData() == buffer.constData()); array.resize(5); - QVERIFY(array.constData() == buffer.constData()); + QVERIFY(array.constData() != buffer.constData()); + // check null termination + QVERIFY(array.constData()[5] == 0); } void tst_QString::resizeAfterReserve() |