From 45bf63676d7dc74bfefb1a9c949c132de91538a7 Mon Sep 17 00:00:00 2001 From: Dennis Oberst Date: Wed, 16 Aug 2023 18:25:41 +0200 Subject: [PATCH] QArrayDataPointer: remove Q_CHECK_PTR in assign(it, it) again This commit reverts 2d77051f9dfd11ae292ad4bac2f28c5f7a0e7f83. When requesting an allocation of size 0, we will actually get a nullptr. qarraydata.cpp: ~~~ if (capacity == 0) { *dptr = nullptr; return nullptr; } This will let the Q_CHECK_PTR trigger falsely. Such an occurrence was initially detected during the cmake_automoc_parser build-step. Found-by: Marc Mutz Task-number: QTBUG-106196 Change-Id: Icb68c5dd518c9623119a61d5c4fdcff43dc4ac5d Reviewed-by: Thiago Macieira (cherry picked from commit 3db9ef358da817480d6f4c055d85a6aa7be17991) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/tools/qarraydatapointer.h | 2 -- tests/auto/corelib/tools/qlist/tst_qlist.cpp | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 258e7b7446b..a77db21c99b 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -327,12 +327,10 @@ public: const qsizetype n = std::distance(first, last); if (needsDetach() || n > constAllocatedCapacity()) { QArrayDataPointer allocated(Data::allocate(detachCapacity(n))); - Q_CHECK_PTR(allocated.data()); swap(allocated); } } else if (needsDetach()) { QArrayDataPointer allocated(Data::allocate(allocatedCapacity())); - Q_CHECK_PTR(allocated.data()); swap(allocated); // We don't want to copy data that we know we'll overwrite } diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index afced64b42c..2b910e2a8ee 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -231,6 +231,7 @@ private slots: void appendCustom() const { append(); } void appendRvalue() const; void appendList() const; + void assignEmpty() const; void assignInt() const { assign(); } void assignMovable() const { assign(); } void assignCustom() const { assign(); } @@ -762,6 +763,25 @@ void tst_QList::append() const } } +void tst_QList::assignEmpty() const +{ + // Test that the realloc branch in assign(it, it) doesn't crash. + using T = int; + QList list; + QList ref1 = list; + QVERIFY(list.d.needsDetach()); + list.assign(list.begin(), list.begin()); + +#if !defined Q_OS_QNX // QNX has problems with the empty istream_iterator + auto empty = std::istream_iterator{}; + list.squeeze(); + QCOMPARE_EQ(list.capacity(), 0); + ref1 = list; + QVERIFY(list.d.needsDetach()); + list.assign(empty, empty); +#endif +} + template void tst_QList::assign() const {