From 7a717aaf459476f4a1f091b0e1d832c592f2caf1 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Fri, 10 Feb 2017 15:37:12 +0100 Subject: QImageWriter: Detect failure due to trying to write a QImage() This is not a bug in QImageWriter, but caller code. We should be explicit about what the problem was so it can be fixed. [ChangeLog][QtGui][QImageWriter] Add QImageWriter::InvalidImageError to communicate invalid attempts to write a bad QImage (for instance, a null QImage). Change-Id: I0333b8263f1da1c672bed17dab48bfd6cafe41a2 Reviewed-by: Eirik Aavitsland --- src/gui/image/qimagewriter.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/gui/image/qimagewriter.cpp') diff --git a/src/gui/image/qimagewriter.cpp b/src/gui/image/qimagewriter.cpp index ab15d8ee29..a39d204677 100644 --- a/src/gui/image/qimagewriter.cpp +++ b/src/gui/image/qimagewriter.cpp @@ -87,6 +87,9 @@ \value UnsupportedFormatError Qt does not support the requested image format. + \value InvalidImageError An attempt was made to write an invalid QImage. An + example of an invalid image would be a null QImage. + \value UnknownError An unknown error occurred. If you get this value after calling write(), it is most likely caused by a bug in QImageWriter. @@ -736,6 +739,13 @@ extern void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orie */ bool QImageWriter::write(const QImage &image) { + // Do this before canWrite, so it doesn't create a file if this fails. + if (Q_UNLIKELY(image.isNull())) { + d->imageWriterError = QImageWriter::InvalidImageError; + d->errorString = QImageWriter::tr("Image is empty"); + return false; + } + if (!canWrite()) return false; -- cgit v1.2.3