diff options
author | Eirik Aavitsland <eirik.aavitsland@qt.io> | 2021-11-19 11:52:12 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-11-23 06:57:01 +0000 |
commit | 94016862a7c2671b876da15b1f306ae22e15ad1d (patch) | |
tree | 05dae24265ff6781d76b393293fdeecb9396a189 | |
parent | 03fdef133e213677c542c33ae372f7ca2631e61f (diff) |
QImageReader: check allocation limit for minimum 32 bpp
Also, as a driveby, add an environment variable so the limit can be
changed at runtime.
[ChangeLog][QtGui][QImageReader] When checking allocation limit during
image reading, the memory requirements are now calculated for a
minimum of 32 bits per pixel, since Qt will typically convert an image
to that depth when it is used in GUI. This means that the effective
allocation limit is significantly smaller when reading 1 bpp and 8 bpp
images.
Change-Id: If1b204d413973b0975eea531e29c260fdcec931d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 8ce36938569841020daf9dc23e41438b06e0ee53)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/gui/image/qimageiohandler.cpp | 2 | ||||
-rw-r--r-- | src/gui/image/qimagereader.cpp | 13 |
2 files changed, 13 insertions, 2 deletions
diff --git a/src/gui/image/qimageiohandler.cpp b/src/gui/image/qimageiohandler.cpp index 75a2f2ac65..8c74de4256 100644 --- a/src/gui/image/qimageiohandler.cpp +++ b/src/gui/image/qimageiohandler.cpp @@ -580,7 +580,7 @@ bool QImageIOHandler::allocateImage(QSize size, QImage::Format format, QImage *i image->detach(); } else { if (const int mbLimit = QImageReader::allocationLimit()) { - qsizetype depth = qt_depthForFormat(format); + qsizetype depth = qMax(qt_depthForFormat(format), 32); // Effective gui depth = 32 QImageData::ImageSizeParameters szp = QImageData::calculateImageParameters(size.width(), size.height(), depth); if (!szp.isValid()) diff --git a/src/gui/image/qimagereader.cpp b/src/gui/image/qimagereader.cpp index 8af36f911b..dbfcb4ee8c 100644 --- a/src/gui/image/qimagereader.cpp +++ b/src/gui/image/qimagereader.cpp @@ -1584,7 +1584,13 @@ QList<QByteArray> QImageReader::imageFormatsForMimeType(const QByteArray &mimeTy */ int QImageReader::allocationLimit() { - return QImageReaderPrivate::maxAlloc; + static int envLimit = []() { + bool ok = false; + int res = qEnvironmentVariableIntValue("QT_IMAGEIO_MAXALLOC", &ok); + return ok ? res : -1; + }(); + + return envLimit >= 0 ? envLimit : QImageReaderPrivate::maxAlloc; } /*! @@ -1592,11 +1598,16 @@ int QImageReader::allocationLimit() Sets the allocation limit to \a mbLimit megabytes. Images that would require a QImage memory allocation above this limit will be rejected. + If \a mbLimit is 0, the allocation size check will be disabled. This limit helps applications avoid unexpectedly large memory usage from loading corrupt image files. It is normally not needed to change it. The default limit is large enough for all commonly used image sizes. + \note The memory requirements are calculated for a minimum of 32 bits per pixel, since Qt will + typically convert an image to that depth when it is used in GUI. This means that the effective + allocation limit is significantly smaller than \a mbLimit when reading 1 bpp and 8 bpp images. + \sa allocationLimit() */ void QImageReader::setAllocationLimit(int mbLimit) |