diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-07-14 12:56:27 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-07-31 20:08:50 +0200 |
commit | 0c53f8ba98fee41d84362b44eb731ff722c8f7fe (patch) | |
tree | 7b46fbac2915998a676c32d172fc11c35fbdaf75 /src | |
parent | 797e18118bc74597f1211e993f58451aaa6f081c (diff) |
Fix undefined use of memcpy and memcmp
Don't call them on a nullptr, even with a length of 0.
Change-Id: I7fee23303562e5771697a16365cae12e3e87af6f
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 11 | ||||
-rw-r--r-- | src/corelib/text/qstring.cpp | 3 | ||||
-rw-r--r-- | src/corelib/text/qstringbuilder.h | 9 | ||||
-rw-r--r-- | src/corelib/tools/qhash.h | 3 |
4 files changed, 17 insertions, 9 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 5b1016e838..aee22edc6f 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -411,9 +411,11 @@ int qstrnicmp(const char *str1, qsizetype len1, const char *str2, qsizetype len2 */ int QtPrivate::compareMemory(QByteArrayView lhs, QByteArrayView rhs) { - int ret = memcmp(lhs.data(), rhs.data(), qMin(lhs.size(), rhs.size())); - if (ret != 0) - return ret; + if (!lhs.isNull() && !rhs.isNull()) { + int ret = memcmp(lhs.data(), rhs.data(), qMin(lhs.size(), rhs.size())); + if (ret != 0) + return ret; + } // they matched qMin(l1, l2) bytes // so the longer one is lexically after the shorter one @@ -1638,7 +1640,8 @@ void QByteArray::reallocData(uint alloc, Data::ArrayOptions options) { if (d->needsDetach()) { DataPointer dd(Data::allocate(alloc, options), qMin(qsizetype(alloc) - 1, d.size)); - ::memcpy(dd.data(), d.data(), dd.size); + if (dd.size > 0) + ::memcpy(dd.data(), d.data(), dd.size); dd.data()[dd.size] = 0; d = dd; } else { diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 0334583900..b63b980e72 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2364,7 +2364,8 @@ void QString::reallocData(size_t alloc, bool grow) if (d->needsDetach()) { DataPointer dd(Data::allocate(alloc, allocOptions), qMin(qsizetype(alloc) - 1, d.size)); - ::memcpy(dd.data(), d.data(), dd.size * sizeof(QChar)); + if (dd.size > 0) + ::memcpy(dd.data(), d.data(), dd.size * sizeof(QChar)); dd.data()[dd.size] = 0; d = dd; } else { diff --git a/src/corelib/text/qstringbuilder.h b/src/corelib/text/qstringbuilder.h index 6ba01c0e30..512b7e7bf7 100644 --- a/src/corelib/text/qstringbuilder.h +++ b/src/corelib/text/qstringbuilder.h @@ -260,7 +260,8 @@ template <> struct QConcatenable<QString> : private QAbstractConcatenable static inline void appendTo(const QString &a, QChar *&out) { const int n = a.size(); - memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); + if (n) + memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); out += n; } }; @@ -274,7 +275,8 @@ template <> struct QConcatenable<QStringRef> : private QAbstractConcatenable static inline void appendTo(const QStringRef &a, QChar *&out) { const int n = a.size(); - memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); + if (n) + memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); out += n; } }; @@ -288,7 +290,8 @@ template <> struct QConcatenable<QStringView> : private QAbstractConcatenable static inline void appendTo(QStringView a, QChar *&out) { const auto n = a.size(); - memcpy(out, a.data(), sizeof(QChar) * n); + if (n) + memcpy(out, a.data(), sizeof(QChar) * n); out += n; } }; diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index eacc373ee3..2b1b155efb 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -397,7 +397,8 @@ struct Span { // we only add storage if the previous storage was fully filled, so // simply copy the old data over if constexpr (isRelocatable<Node>()) { - memcpy(newEntries, entries, allocated*sizeof(Entry)); + if (allocated) + memcpy(newEntries, entries, allocated*sizeof(Entry)); } else { for (size_t i = 0; i < allocated; ++i) { new (&newEntries[i].node()) Node(std::move(entries[i].node())); |