From f6f68409a40beaa5f034c411dd7e296c7828d8fd Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 13 Nov 2020 12:33:15 +0100 Subject: [PATCH] Don't initialize POD types with memset(0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is dangerous as at least pointers to member objects/data are not always zero initialized. Change-Id: I1250e101ab73cd816694315fc9130f4d486b9feb Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim --- src/corelib/global/qglobal.cpp | 6 ++---- src/corelib/tools/qarraydataops.h | 6 ++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 48eb5b4567..ad17f41236 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -3970,10 +3970,8 @@ bool qunsetenv(const char *varName) \list \li \c Q_PRIMITIVE_TYPE specifies that \a Type is a POD (plain old - data) type with no constructor or destructor, or else a type where - every bit pattern is a valid object; memset()ting memory to zero - creates a value-initialized instance of the type; and memcpy()ing - creates a valid independent copy of an object. + data) type with no constructor or destructor, or else a type memcpy()ing + creates a valid independent copy of the object. \li \c Q_RELOCATABLE_TYPE specifies that \a Type has a constructor and/or a destructor but can be moved in memory using \c memcpy(). diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index c1ae93dc10..5f43ed3dc0 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -83,8 +83,11 @@ public: Q_ASSERT(newSize > this->size); Q_ASSERT(newSize - this->size <= this->freeSpaceAtEnd()); - ::memset(static_cast(this->end()), 0, (newSize - this->size) * sizeof(T)); + T *where = this->end(); this->size = qsizetype(newSize); + const T *e = this->end(); + while (where != e) + *where++ = T(); } void copyAppend(const T *b, const T *e) noexcept @@ -675,7 +678,6 @@ protected: using DataPointer = QArrayDataPointer; public: - // using QGenericArrayOps::appendInitialize; // using QGenericArrayOps::copyAppend; // using QGenericArrayOps::moveAppend; // using QGenericArrayOps::truncate;