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>
bb10
Allan Sandfeld Jensen 2020-07-14 12:56:27 +02:00
parent 797e18118b
commit 0c53f8ba98
4 changed files with 17 additions and 9 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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;
}
};

View File

@ -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()));