QMimeMagicRule: fix UB (misaligned load) in matchNumber<T>()

Found by UBSan:

  qmimemagicrule.cpp:166:53: runtime error: load of misaligned address 0x00000124bcb9 for type 'const short unsigned int', which requires 2 byte alignment
  qmimemagicrule.cpp:166:53: runtime error: load of misaligned address 0x00000124bcb9 for type 'const unsigned int', which requires 4 byte alignment

Fix by using new qUnalignedLoad<T>() instead of a
load through a type-punned pointer and misaligned
pointer.

Change-Id: I6b876f1ce7e01369fbb25a51263d1ad04be07d52
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Marc Mutz 2016-03-10 09:53:36 +01:00
parent 62e0a98282
commit fb196e8807

View File

@ -42,6 +42,7 @@
#include <QtCore/QList>
#include <QtCore/QDebug>
#include <qendian.h>
#include <private/qsimd_p.h> // for qUnalignedLoad
QT_BEGIN_NAMESPACE
@ -176,7 +177,7 @@ static bool matchNumber(const QMimeMagicRulePrivate *d, const QByteArray &data)
const char *p = data.constData() + d->startPos;
const char *e = data.constData() + qMin(data.size() - int(sizeof(T)), d->endPos + 1);
for ( ; p <= e; ++p) {
if ((*reinterpret_cast<const T*>(p) & mask) == (value & mask))
if ((qUnalignedLoad<T>(p) & mask) == (value & mask))
return true;
}