summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2016-03-10 09:53:36 +0100
committerMarc Mutz <marc.mutz@kdab.com>2016-03-10 10:57:53 +0000
commitfb196e88074a8547ef93030ee385227664965106 (patch)
tree11cf2463da49874c45aa0b19226e615ed070e1c6
parent62e0a98282081911616a8c005464d483a3a480d2 (diff)
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>
-rw-r--r--src/corelib/mimetypes/qmimemagicrule.cpp3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/corelib/mimetypes/qmimemagicrule.cpp b/src/corelib/mimetypes/qmimemagicrule.cpp
index 6a3a429179..44834420fe 100644
--- a/src/corelib/mimetypes/qmimemagicrule.cpp
+++ b/src/corelib/mimetypes/qmimemagicrule.cpp
@@ -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;
}