From a219b8f3822a00e9bc2ae18419fa774355bb90b3 Mon Sep 17 00:00:00 2001 From: "Bradley T. Hughes" Date: Fri, 23 Sep 2011 12:29:18 +0200 Subject: Fix QString and QByteArray reserve() and squeeze() These functions should not take care not to unconditionally set the capacityReserved private member, since the d may be referencing the const shared_null or shared_empty which live in read-only memory. The squeeze() methods check for ref > 1 instead of ref != 1 to prevent detaching from the shared_null/shared_empty unnecessarily; the shared_null/shared_empty ref count is -1, meaning squeeze() will never detach from it. Change-Id: Id3f1725a6f08b3a462343640a47bbe78f08ca7e7 Rubberstamped-by: Lars Knoll Reviewed-on: http://codereview.qt-project.org/5454 Reviewed-by: Qt Sanity Bot Reviewed-by: Lars Knoll --- tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp | 6 ++++++ tests/auto/corelib/tools/qstring/tst_qstring.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+) (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index 5f036f37fe..dc0c8905fd 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -1536,6 +1536,12 @@ void tst_QByteArray::reserve() QVERIFY(capacity == qba.capacity()); QVERIFY(data == qba.data()); } + + QByteArray nil1, nil2; + nil1.reserve(0); + nil2.squeeze(); + nil1.squeeze(); + nil2.reserve(0); } void tst_QByteArray::literals() diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index 5ef8d70ef6..dc5070c9c2 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -224,6 +224,8 @@ private slots: void toUpperLower_icu(); void literals(); + + void reserve(); }; typedef QList IntList; @@ -5137,6 +5139,14 @@ void tst_QString::literals() #endif } +void tst_QString::reserve() +{ + QString nil1, nil2; + nil1.reserve(0); + nil2.squeeze(); + nil1.squeeze(); + nil2.reserve(0); +} QTEST_APPLESS_MAIN(tst_QString) -- cgit v1.2.3