diff options
author | Andrei Golubev <andrei.golubev@qt.io> | 2021-04-25 12:36:46 +0200 |
---|---|---|
committer | Andrei Golubev <andrei.golubev@qt.io> | 2021-04-27 14:12:34 +0200 |
commit | 15a1ec21528b8851a943698310c18742f1983094 (patch) | |
tree | 1fcd934ca3ede9df18ef94e0249dfd7e63964d8d /src/corelib/tools/qarraydataops.h | |
parent | e31a0d06d841deb0ad412c81e383759b8f578529 (diff) |
Do not shift the data pointer when removing all elements from QList
Because leaving the pointer untouched is a much more expected behavior
The tests for this (and not only) logic can be found in the following commit
Change-Id: Iec9eec9bbce04c9fd90cb6be9627c135cd989b7f
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
(cherry picked from commit 38bba2a87c6c5c2b8100870add6d0d7ad559e669)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/corelib/tools/qarraydataops.h')
-rw-r--r-- | src/corelib/tools/qarraydataops.h | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index b70e8e4d9a..cffe1b4575 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -231,10 +231,12 @@ public: // are invalidated. However, erasing from the beginning effectively // means that all iterators are invalidated. We can use this freedom to // erase by moving towards the end. - if (b == this->begin()) + if (b == this->begin() && e != this->end()) { this->ptr = e; - else if (e != this->end()) - ::memmove(static_cast<void *>(b), static_cast<void *>(e), (static_cast<T *>(this->end()) - e) * sizeof(T)); + } else if (e != this->end()) { + ::memmove(static_cast<void *>(b), static_cast<void *>(e), + (static_cast<T *>(this->end()) - e) * sizeof(T)); + } this->size -= n; } @@ -610,7 +612,7 @@ public: // are invalidated. However, erasing from the beginning effectively // means that all iterators are invalidated. We can use this freedom to // erase by moving towards the end. - if (b == this->begin()) { + if (b == this->begin() && e != this->end()) { this->ptr = e; } else { const T *const end = this->end(); @@ -840,7 +842,7 @@ public: // erase by moving towards the end. std::destroy(b, e); - if (b == this->begin()) { + if (b == this->begin() && e != this->end()) { this->ptr = e; } else if (e != this->end()) { memmove(static_cast<void *>(b), static_cast<const void *>(e), (static_cast<const T *>(this->end()) - e)*sizeof(T)); |