From 504972f838761f79a170c22225add496e7e5af6a Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Tue, 3 Nov 2020 11:26:20 +0100 Subject: Refine {QString, QBA}::reallocData() logic Fixed misleading naming of "slowReallocatePath". It's no longer "slow", it's downright dangerous now to reallocate under certain conditions While at it, added extra assert to QArrayData::reallocateUnaligned() and cleaned up that function a bit Change-Id: I05921fb5058eb563997e66107566c87fb4ea5599 Reviewed-by: Lars Knoll Reviewed-by: Thiago Macieira --- src/corelib/tools/qarraydata.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/corelib/tools/qarraydata.cpp') diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index 2bf3e9bacc2..e73dc358106 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -233,10 +233,13 @@ QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, { Q_ASSERT(!data || !data->isShared()); - qsizetype headerSize = sizeof(QArrayData); + const qsizetype headerSize = sizeof(QArrayData); qsizetype allocSize = calculateBlockSize(capacity, objectSize, headerSize, option); - qptrdiff offset = dataPointer ? reinterpret_cast(dataPointer) - reinterpret_cast(data) : headerSize; + const qptrdiff offset = dataPointer + ? reinterpret_cast(dataPointer) - reinterpret_cast(data) + : headerSize; Q_ASSERT(offset > 0); + Q_ASSERT(offset <= allocSize); // == when all free space is at the beginning allocSize = reserveExtraBytes(allocSize); if (Q_UNLIKELY(allocSize < 0)) // handle overflow. cannot reallocate reliably @@ -244,7 +247,7 @@ QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, QArrayData *header = static_cast(::realloc(data, size_t(allocSize))); if (header) { - header->alloc = uint(capacity); + header->alloc = capacity; dataPointer = reinterpret_cast(header) + offset; } else { dataPointer = nullptr; -- cgit v1.2.3