diff --git a/src/corelib/mimetypes/qmimeprovider.cpp b/src/corelib/mimetypes/qmimeprovider.cpp index 46cf6f77759..00d1f2636d4 100644 --- a/src/corelib/mimetypes/qmimeprovider.cpp +++ b/src/corelib/mimetypes/qmimeprovider.cpp @@ -718,10 +718,24 @@ void QMimeXMLProvider::findByMagic(const QByteArray &data, QMimeMagicResult &res for (const QMimeMagicRuleMatcher &matcher : std::as_const(m_magicMatchers)) { if (matcher.matches(data)) { const int priority = matcher.priority(); - if (priority > result.accuracy) { - result.accuracy = priority; - result.candidate = matcher.mimetype(); + if (priority < result.accuracy) + continue; + if (priority == result.accuracy) { + if (m_db->inherits(result.candidate, matcher.mimetype())) + continue; + + if (!m_db->inherits(matcher.mimetype(), result.candidate)) { + // Two or more magic rules matching, both with the same priority but not + // connected with one another should not happen: + qWarning("QMimeXMLProvider: MimeType is ambiguous between %ls and %ls", + qUtf16Printable(result.candidate), + qUtf16Printable(matcher.mimetype())); + continue; + } } + + result.accuracy = priority; + result.candidate = matcher.mimetype(); } } } diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 8cf3e9f5ac1..70314bd29cb 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -646,6 +646,9 @@ void tst_QMimeDatabase::mimeTypeForData_data() else QTest::newRow("diff_space") << QByteArray("diff ") << "text/x-diff"; QTest::newRow("unknown") << QByteArray("\001abc?}") << "application/octet-stream"; + QTest::newRow("ambigous svg/xml") << QByteArray(R"( + +)") << "image/svg+xml"; } void tst_QMimeDatabase::mimeTypeForData()