commit 880a4e8512343462353f69ddecb63ae1748b9902 Author: Peter Penz <peter.penz19@gmail.com> Date: Sat Aug 20 19:14:28 2011 +0200 Don't hang when determining MIME type of corrupted files The patch has been provided by Miroslav Ľos. BUG: 280446 diff --git a/kdecore/services/kmimetype.cpp b/kdecore/services/kmimetype.cpp index 955bf62..a4631f2 100644 --- a/kdecore/services/kmimetype.cpp +++ b/kdecore/services/kmimetype.cpp @@ -218,7 +218,7 @@ KMimeType::Ptr KMimeType::findByUrlHelper( const KUrl& _url, mode_t mode, } if ( device && !device->isOpen() ) { - if ( !device->open(QIODevice::ReadOnly) ) { + if ( !device->open(QIODevice::ReadOnly|QIODevice::Unbuffered) ) { device = 0; } } @@ -383,7 +383,7 @@ KMimeType::Ptr KMimeType::findByFileContent( const QString &fileName, int *accur *accuracy = 100; return mimeFromMode; } - if (!device.open(QIODevice::ReadOnly)) { + if (!device.open(QIODevice::ReadOnly|QIODevice::Unbuffered)) { if (accuracy) *accuracy = 0; return KMimeType::defaultMimeTypePtr(); diff --git a/kdecore/services/kmimetyperepository.cpp b/kdecore/services/kmimetyperepository.cpp index 6ff3d16..7a69005 100644 --- a/kdecore/services/kmimetyperepository.cpp +++ b/kdecore/services/kmimetyperepository.cpp @@ -234,6 +234,14 @@ KMimeType::Ptr KMimeTypeRepository::findFromContent(QIODevice* device, int* accu *accuracy = 100; return findMimeTypeByName(QLatin1String("application/x-zerosize")); } + if (beginning.isEmpty()) { + // check if we can really read the data; also provide enough data for most rules + const qint64 dataNeeded = qMin(deviceSize, (qint64) 2048); + beginning.resize(dataNeeded); + if (!device->seek(0) || device->read(beginning.data(), dataNeeded) == -1) { + return defaultMimeTypePtr(); // don't bother detecting unreadable file + } + } m_mutex.lockForWrite(); if (!m_magicFilesParsed) {