Skip to content

Commit 3280fef

Browse files
Pauli JarvinenPasi Pentikäinen
authored andcommitted
Add out-of-memory checks to QImage
Some of the methods of QImage crashed on out-of-memory situation as they referred to null pointer if creation of a new QImage instance had failed. Many cases like this were already fixed by commit a041e4e which was a fix for QTBUG-1985, but few cases still remained. Now, all the lines creating a new QImage instance in qimage.cpp have been gone through and sanity checks have been added where it was necessary to avoid immediate crashing. Task-number: ou1cimx1#994957 Change-Id: I1f07e8890bc91de18af075be73b1a06c667f3776 Reviewed-by: Murray Read <[email protected]> Reviewed-by: Pasi Pentikäinen <[email protected]> (cherry picked from commit 35aff3e)
1 parent 67ed6f4 commit 3280fef

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/gui/image/qimage.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3942,6 +3942,7 @@ static inline int closestMatch(QRgb pixel, const QVector<QRgb> &clut) {
39423942
static QImage convertWithPalette(const QImage &src, QImage::Format format,
39433943
const QVector<QRgb> &clut) {
39443944
QImage dest(src.size(), format);
3945+
QIMAGE_SANITYCHECK_MEMORY(dest);
39453946
dest.setColorTable(clut);
39463947

39473948
#if !defined(QT_NO_IMAGE_TEXT)
@@ -4290,6 +4291,7 @@ QImage QImage::convertBitOrder(Endian bitOrder) const
42904291
return *this;
42914292

42924293
QImage image(d->width, d->height, d->format == Format_Mono ? Format_MonoLSB : Format_Mono);
4294+
QIMAGE_SANITYCHECK_MEMORY(image);
42934295

42944296
const uchar *data = d->data;
42954297
const uchar *end = data + d->nbytes;
@@ -4917,6 +4919,7 @@ QImage QImage::rgbSwapped() const
49174919
case Format_MonoLSB:
49184920
case Format_Indexed8:
49194921
res = copy();
4922+
QIMAGE_SANITYCHECK_MEMORY(res);
49204923
for (int i = 0; i < res.d->colortable.size(); i++) {
49214924
QRgb c = res.d->colortable.at(i);
49224925
res.d->colortable[i] = QRgb(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00));
@@ -6172,6 +6175,10 @@ void QImage::setAlphaChannel(const QImage &alphaChannel)
61726175

61736176
} else {
61746177
const QImage sourceImage = alphaChannel.convertToFormat(QImage::Format_RGB32);
6178+
if (sourceImage.isNull()) {
6179+
qWarning("QImage::setAlphaChannel: out of memory");
6180+
return;
6181+
}
61756182
const uchar *src_data = sourceImage.d->data;
61766183
const uchar *dest_data = d->data;
61776184
for (int y=0; y<h; ++y) {
@@ -6224,6 +6231,7 @@ QImage QImage::alphaChannel() const
62246231
int h = d->height;
62256232

62266233
QImage image(w, h, Format_Indexed8);
6234+
QIMAGE_SANITYCHECK_MEMORY(image);
62276235
image.setColorCount(256);
62286236

62296237
// set up gray scale table.
@@ -6253,6 +6261,7 @@ QImage QImage::alphaChannel() const
62536261
QImage alpha32 = *this;
62546262
if (d->format != Format_ARGB32 && d->format != Format_ARGB32_Premultiplied)
62556263
alpha32 = convertToFormat(Format_ARGB32);
6264+
QIMAGE_SANITYCHECK_MEMORY(alpha32);
62566265

62576266
const uchar *src_data = alpha32.d->data;
62586267
uchar *dest_data = image.d->data;
@@ -6396,6 +6405,7 @@ static QImage smoothScaled(const QImage &source, int w, int h) {
63966405

63976406
static QImage rotated90(const QImage &image) {
63986407
QImage out(image.height(), image.width(), image.format());
6408+
QIMAGE_SANITYCHECK_MEMORY(out);
63996409
if (image.colorCount() > 0)
64006410
out.setColorTable(image.colorTable());
64016411
int w = image.width();
@@ -6455,6 +6465,7 @@ static QImage rotated180(const QImage &image) {
64556465

64566466
static QImage rotated270(const QImage &image) {
64576467
QImage out(image.height(), image.width(), image.format());
6468+
QIMAGE_SANITYCHECK_MEMORY(out);
64586469
if (image.colorCount() > 0)
64596470
out.setColorTable(image.colorTable());
64606471
int w = image.width();

0 commit comments

Comments
 (0)