Mime database: Fix potential memory leak.

Uncovered by code scanning.
This commit is contained in:
Friedemann Kleint
2010-01-25 10:09:19 +01:00
parent 971e4b596c
commit 253f8c6010

View File

@@ -93,6 +93,9 @@ enum { BinaryMatchPriority = 1, TextMatchPriority = 2};
*/ */
namespace Core { namespace Core {
typedef QSharedPointer<MagicRuleMatcher> MagicRuleMatcherPtr;
namespace Internal { namespace Internal {
// FileMatchContext: Passed on to the mimetypes from the database // FileMatchContext: Passed on to the mimetypes from the database
@@ -213,7 +216,10 @@ bool MagicRule::matches(const QByteArray &data) const
return data.startsWith(m_pattern); return data.startsWith(m_pattern);
// Range // Range
const int index = data.indexOf(m_pattern, m_startPos); const int index = data.indexOf(m_pattern, m_startPos);
return index != -1 && index < m_endPos; const bool rc = index != -1 && index < m_endPos;
if (debugMimeDB)
qDebug() << "Checking " << m_pattern << m_startPos << m_endPos << " returns " << rc;
return rc;
} }
MagicRule *MagicRule::createStringRule(const QString &c, int startPos, int endPos) MagicRule *MagicRule::createStringRule(const QString &c, int startPos, int endPos)
@@ -657,7 +663,7 @@ static bool parseNumber(const QString &n, int *target, QString *errorMessage)
// <match value="must be converted with BinHex" type="string" offset="11"/> // <match value="must be converted with BinHex" type="string" offset="11"/>
// <match value="0x9501" type="big16" offset="0:64"/> // <match value="0x9501" type="big16" offset="0:64"/>
static bool addMagicMatchRule(const QXmlStreamAttributes &atts, static bool addMagicMatchRule(const QXmlStreamAttributes &atts,
MagicRuleMatcher *ruleMatcher, const MagicRuleMatcherPtr &ruleMatcher,
QString *errorMessage) QString *errorMessage)
{ {
const QString type = atts.value(QLatin1String(matchTypeAttributeC)).toString(); const QString type = atts.value(QLatin1String(matchTypeAttributeC)).toString();
@@ -687,8 +693,7 @@ static bool addMagicMatchRule(const QXmlStreamAttributes &atts,
bool BaseMimeTypeParser::parse(QIODevice *dev, const QString &fileName, QString *errorMessage) bool BaseMimeTypeParser::parse(QIODevice *dev, const QString &fileName, QString *errorMessage)
{ {
MimeTypeData data; MimeTypeData data;
MagicRuleMatcher *ruleMatcher = 0; MagicRuleMatcherPtr ruleMatcher;
QXmlStreamReader reader(dev); QXmlStreamReader reader(dev);
ParseStage ps = ParseBeginning; ParseStage ps = ParseBeginning;
while (!reader.atEnd()) { while (!reader.atEnd()) {
@@ -739,11 +744,12 @@ bool BaseMimeTypeParser::parse(QIODevice *dev, const QString &fileName, QString
return false; return false;
} }
ruleMatcher = new MagicRuleMatcher; ruleMatcher = MagicRuleMatcherPtr(new MagicRuleMatcher);
ruleMatcher->setPriority(priority); ruleMatcher->setPriority(priority);
} }
break; break;
case ParseMagicMatchRule: case ParseMagicMatchRule:
QTC_ASSERT(!ruleMatcher.isNull(), return false)
if (!addMagicMatchRule(reader.attributes(), ruleMatcher, errorMessage)) if (!addMagicMatchRule(reader.attributes(), ruleMatcher, errorMessage))
return false; return false;
break; break;
@@ -762,9 +768,10 @@ bool BaseMimeTypeParser::parse(QIODevice *dev, const QString &fileName, QString
data.clear(); data.clear();
} else { } else {
// Finished a match sequence // Finished a match sequence
if (reader.name() == QLatin1String(QLatin1String(magicTagC))) { if (reader.name() == QLatin1String(magicTagC)) {
data.magicMatchers.push_back(QSharedPointer<IMagicMatcher>(ruleMatcher)); QTC_ASSERT(!ruleMatcher.isNull(), return false)
ruleMatcher = 0; data.magicMatchers.push_back(ruleMatcher);
ruleMatcher = MagicRuleMatcherPtr();
} }
} }
break; break;