Skip to content

Commit

Permalink
QImageReader: check allocation limit for minimum 32 bpp
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
aavit committed Nov 22, 2021
1 parent f1c280f commit 8ce3693
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/gui/image/qimageiohandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
13 changes: 12 additions & 1 deletion src/gui/image/qimagereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,19 +1584,30 @@ 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;
}

/*!
\since 6.0
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)
Expand Down

0 comments on commit 8ce3693

Please sign in to comment.