MimeDatabase: Do not crash/assert on invalid mime magic data

Change-Id: I355241e472b1bb379ccc94fdd896c6d894206b0a
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
Eike Ziller
2015-03-05 10:33:01 +01:00
parent a1abbb983b
commit 673be1a3f8
3 changed files with 43 additions and 16 deletions

View File

@@ -233,14 +233,13 @@ QByteArray MimeMagicRule::makePattern(const QByteArray &value)
} }
MimeMagicRule::MimeMagicRule(MimeMagicRule::Type theType, MimeMagicRule::MimeMagicRule(MimeMagicRule::Type theType,
const QByteArray &theValue, const QByteArray &theValue,
int theStartPos, int theStartPos,
int theEndPos, int theEndPos,
const QByteArray &theMask) : const QByteArray &theMask,
QString *errorString) :
d(new MimeMagicRulePrivate) d(new MimeMagicRulePrivate)
{ {
Q_ASSERT(!theValue.isEmpty());
d->type = theType; d->type = theType;
d->value = theValue; d->value = theValue;
d->startPos = theStartPos; d->startPos = theStartPos;
@@ -248,10 +247,23 @@ MimeMagicRule::MimeMagicRule(MimeMagicRule::Type theType,
d->mask = theMask; d->mask = theMask;
d->matchFunction = 0; d->matchFunction = 0;
if (d->value.isEmpty()) {
d->type = Invalid;
if (errorString)
*errorString = QLatin1String("Invalid empty magic rule value");
return;
}
if (d->type >= Host16 && d->type <= Byte) { if (d->type >= Host16 && d->type <= Byte) {
bool ok; bool ok;
d->number = d->value.toUInt(&ok, 0); // autodetect d->number = d->value.toUInt(&ok, 0); // autodetect
Q_ASSERT(ok); if (!ok) {
d->type = Invalid;
if (errorString)
*errorString = QString::fromLatin1("Invalid magic rule value \"%1\"").arg(
QString::fromLatin1(d->value));
return;
}
d->numberMask = !d->mask.isEmpty() ? d->mask.toUInt(&ok, 0) : 0; // autodetect d->numberMask = !d->mask.isEmpty() ? d->mask.toUInt(&ok, 0) : 0; // autodetect
} }
@@ -260,9 +272,23 @@ MimeMagicRule::MimeMagicRule(MimeMagicRule::Type theType,
d->pattern = makePattern(d->value); d->pattern = makePattern(d->value);
d->pattern.squeeze(); d->pattern.squeeze();
if (!d->mask.isEmpty()) { if (!d->mask.isEmpty()) {
Q_ASSERT(d->mask.size() >= 4 && d->mask.startsWith("0x")); if (d->mask.size() < 4 || !d->mask.startsWith("0x")) {
d->mask = QByteArray::fromHex(QByteArray::fromRawData(d->mask.constData() + 2, d->mask.size() - 2)); d->type = Invalid;
Q_ASSERT(d->mask.size() == d->pattern.size()); if (errorString)
*errorString = QString::fromLatin1("Invalid magic rule mask \"%1\"").arg(
QString::fromLatin1(d->mask));
return;
}
const QByteArray &tempMask = QByteArray::fromHex(QByteArray::fromRawData(
d->mask.constData() + 2, d->mask.size() - 2));
if (tempMask.size() != d->pattern.size()) {
d->type = Invalid;
if (errorString)
*errorString = QString::fromLatin1("Invalid magic rule mask size \"%1\"").arg(
QString::fromLatin1(d->mask));
return;
}
d->mask = tempMask;
} else { } else {
d->mask.fill(char(-1), d->pattern.size()); d->mask.fill(char(-1), d->pattern.size());
} }

View File

@@ -65,7 +65,8 @@ class QTCREATOR_UTILS_EXPORT MimeMagicRule
public: public:
enum Type { Invalid = 0, String, Host16, Host32, Big16, Big32, Little16, Little32, Byte }; enum Type { Invalid = 0, String, Host16, Host32, Big16, Big32, Little16, Little32, Byte };
MimeMagicRule(Type type, const QByteArray &value, int startPos, int endPos, const QByteArray &mask = QByteArray()); MimeMagicRule(Type type, const QByteArray &value, int startPos, int endPos,
const QByteArray &mask = QByteArray(), QString *errorString = 0);
MimeMagicRule(const MimeMagicRule &other); MimeMagicRule(const MimeMagicRule &other);
~MimeMagicRule(); ~MimeMagicRule();

View File

@@ -176,10 +176,6 @@ static bool createMagicMatchRule(const QXmlStreamAttributes &atts,
return true; return true;
} }
const QString value = atts.value(QLatin1String(matchValueAttributeC)).toString(); const QString value = atts.value(QLatin1String(matchValueAttributeC)).toString();
if (value.isEmpty()) {
*errorMessage = QString::fromLatin1("Empty match value detected.");
return false;
}
// Parse for offset as "1" or "1:10" // Parse for offset as "1" or "1:10"
int startPos, endPos; int startPos, endPos;
const QString offsetS = atts.value(QLatin1String(matchOffsetAttributeC)).toString(); const QString offsetS = atts.value(QLatin1String(matchOffsetAttributeC)).toString();
@@ -190,8 +186,12 @@ static bool createMagicMatchRule(const QXmlStreamAttributes &atts,
return false; return false;
const QString mask = atts.value(QLatin1String(matchMaskAttributeC)).toString(); const QString mask = atts.value(QLatin1String(matchMaskAttributeC)).toString();
rule = new MimeMagicRule(magicType, value.toUtf8(), startPos, endPos, mask.toLatin1()); MimeMagicRule *tempRule = new MimeMagicRule(magicType, value.toUtf8(), startPos, endPos,
mask.toLatin1(), errorMessage);
if (!tempRule->isValid())
return false;
rule = tempRule;
return true; return true;
} }
#endif #endif