summaryrefslogtreecommitdiffstats
path: root/src/gui/image
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2019-01-11 15:57:56 +0100
committerEirik Aavitsland <eirik.aavitsland@qt.io>2019-07-18 16:05:57 +0200
commit82aabafadad2010f0f20abeeb7234057e3c10e9f (patch)
tree0662cf31cdb38f3c68ba0d917715eec7d9947e69 /src/gui/image
parent7f948d9effad983477977c3274231401f260c531 (diff)
Fix loading of image files with wrong, but known, file name extension
If QImageReader recognized the suffix of a file, it would by default not check if the file contents matched the claimed format. Hence, a valid but misnamed image file would fail to load. Fix by adding contents check for suffix-recognized files. [ChangeLog][QtGui][Image] Loading of image files having a file name suffix for a different image file type has been fixed. QImageReader will now ask the suffix format handler to confirm the file contents (canRead()), and fall back to normal file content recognition on failure. This implies a slight behavior change in QImageReader::loopCount(), ::imageCount() and ::nextImageDelay(): For an unreadable file with a recognized suffix, they would earlier return 0, while they now will return -1, i.e. error as per the documentation. Fixes: QTBUG-42540 Fixes: QTBUG-68787 Change-Id: I205e83f29ed7190cbcae95dab960232544d012f6 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/gui/image')
-rw-r--r--src/gui/image/qimagereader.cpp27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/gui/image/qimagereader.cpp b/src/gui/image/qimagereader.cpp
index 25b9698559..6a0763e696 100644
--- a/src/gui/image/qimagereader.cpp
+++ b/src/gui/image/qimagereader.cpp
@@ -197,7 +197,7 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
#ifdef QIMAGEREADER_DEBUG
qDebug() << "QImageReader::createReadHandler( device =" << (void *)device << ", format =" << format << "),"
- << keyMap.size() << "plugins available: " << keyMap.values();
+ << keyMap.uniqueKeys().size() << "plugins available: " << keyMap;
#endif
int suffixPluginIndex = -1;
@@ -325,6 +325,29 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
#endif
}
+ if (handler && device && !suffix.isEmpty()) {
+ Q_ASSERT(qobject_cast<QFile *>(device));
+ // We have a file claiming to be of a recognized format. Now confirm that
+ // the handler also recognizes the file contents.
+ const qint64 pos = device->pos();
+ handler->setDevice(device);
+ if (!form.isEmpty())
+ handler->setFormat(form);
+ bool canRead = handler->canRead();
+ device->seek(pos);
+ if (canRead) {
+ // ok, we're done.
+ return handler;
+ }
+#ifdef QIMAGEREADER_DEBUG
+ qDebug() << "QImageReader::createReadHandler: the" << suffix << "handler can not read this file";
+#endif
+ // File may still be valid, just with wrong suffix, so fall back to
+ // finding a handler based on contents, below.
+ delete handler;
+ handler = nullptr;
+ }
+
#ifndef QT_NO_IMAGEFORMATPLUGIN
if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
// check if any of our plugins recognize the file from its contents.
@@ -336,7 +359,7 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
if (plugin && plugin->capabilities(device, QByteArray()) & QImageIOPlugin::CanRead) {
handler = plugin->create(device, testFormat);
#ifdef QIMAGEREADER_DEBUG
- qDebug() << "QImageReader::createReadHandler: the" << keyMap.keys().at(i) << "plugin can read this data";
+ qDebug() << "QImageReader::createReadHandler: the" << keyMap.value(i) << "plugin can read this data";
#endif
break;
}