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.

Pick-to: 6.2 6.2.2
Change-Id: If1b204d413973b0975eea531e29c260fdcec931d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Eirik Aavitsland 2021-11-19 11:52:12 +01:00
parent f1c280f31f
commit 8ce3693856
2 changed files with 13 additions and 2 deletions

View File

@ -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())

View File

@ -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)