psd: Protect against broken images

If you have an image that says it's Mono but has 16 as header.depth we
end up doing invalid memory accesses

oss-fuzz/46437
This commit is contained in:
Albert Astals Cid 2022-04-07 00:31:16 +02:00
parent 5c47a97b79
commit 9e28aae868

View File

@ -501,10 +501,8 @@ static QImage::Format imageFormat(const PSDHeader &header)
format = QImage::Format_Indexed8;
break;
case CM_BITMAP:
format = QImage::Format_Mono;
format = header.depth == 1 ? QImage::Format_Mono : QImage::Format_Invalid;
break;
default:
qDebug() << "Unsupported color mode" << header.color_mode;
}
return format;
}
@ -624,7 +622,13 @@ static bool LoadPSD(QDataStream &stream, const PSDHeader &header, QImage &img)
return false;
}
img = QImage(header.width, header.height, imageFormat(header));
const QImage::Format format = imageFormat(header);
if (format == QImage::Format_Invalid) {
qWarning() << "Unsupported image format" << header.color_mode << header.depth;
return false;
}
img = QImage(header.width, header.height, format);
if (img.isNull()) {
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(header.width, header.height);
return false;