From 6239246ea8b30a2fce03c65239cc112d7e338578 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Wed, 2 Sep 2020 15:35:07 +0200 Subject: QCommonArrayOps: append when inserting into empty container Ensured append is chosen instead of prepend for corner cases when inserting into an empty container Mirrored the logic in shouldGrowBeforeInsert function Task-number: QTBUG-84320 Change-Id: I1c963a2588c331029e450fe55001bbf324f65fb4 Reviewed-by: Lars Knoll --- src/corelib/tools/qarraydataops.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index 9fdf0738e1..db795e2413 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -1320,7 +1320,7 @@ public: const qsizetype freeAtEnd = this->freeSpaceAtEnd(); const qsizetype capacity = this->constAllocatedCapacity(); - if (where == this->begin()) { // prepend + if (this->size > 0 && where == this->begin()) { // prepend // Qt5 QList in prepend: not enough space at begin && 33% full // Now (below): return freeAtBegin < n && (this->size >= (capacity / 3)); @@ -1420,7 +1420,7 @@ public: Q_ASSERT(e <= where || b > this->end() || where == this->end()); // No overlap or append Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size); - if (where == this->begin()) { // prepend case - special space arrangement + if (this->size > 0 && where == this->begin()) { // prepend case - special space arrangement prepareSpaceForPrepend(b, e, e - b); // ### perf. loss Base::insert(GrowsBackwardsTag{}, this->begin(), b, e); return; @@ -1445,7 +1445,7 @@ public: Q_ASSERT(where >= this->begin() && where <= this->end()); Q_ASSERT(this->allocatedCapacity() - size_t(this->size) >= n); - if (where == this->begin()) { // prepend case - special space arrangement + if (this->size > 0 && where == this->begin()) { // prepend case - special space arrangement // Preserve the value, because it might be a reference to some part of the moved chunk T tmp(t); prepareSpaceForPrepend(n); // ### perf. loss -- cgit v1.2.3