summaryrefslogtreecommitdiffstats
path: root/src/gui
diff options
context:
space:
mode:
authorKonstantin Ritt <ritt.ks@gmail.com>2012-10-08 16:40:13 +0300
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-10-10 13:24:23 +0200
commit008e5ba61a68846ad29c65a2fe10b8c19c74f6eb (patch)
tree59f1bc75adb09b41b862de4145924368529ef95d /src/gui
parent34c31cd74cde7a1c36f3603a625fb0d28c259a6e (diff)
Invalidate old QImage data if load()/loadFromData() has failed
This guarantees one will never get `!img.isNull()` after load()/loadFromData() has failed, even if the image was not null before. Apply the same fix to QPixmap and QPicture. Change-Id: Ida1ad6a6f0fc830df8e75ada0c163fc2d3360dea Reviewed-by: Samuel Rødal <samuel.rodal@digia.com>
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/image/qimage.cpp29
-rw-r--r--src/gui/image/qpicture.cpp22
-rw-r--r--src/gui/image/qpixmap.cpp34
3 files changed, 42 insertions, 43 deletions
diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp
index bf12dac41d..25999b7d06 100644
--- a/src/gui/image/qimage.cpp
+++ b/src/gui/image/qimage.cpp
@@ -4346,7 +4346,8 @@ QImage QImage::rgbSwapped() const
/*!
Loads an image from the file with the given \a fileName. Returns true if
- the image was successfully loaded; otherwise returns false.
+ the image was successfully loaded; otherwise invalidates the image
+ and returns false.
The loader attempts to read the image using the specified \a format, e.g.,
PNG or JPG. If \a format is not specified (which is the default), the
@@ -4363,15 +4364,9 @@ QImage QImage::rgbSwapped() const
bool QImage::load(const QString &fileName, const char* format)
{
- if (fileName.isEmpty())
- return false;
-
QImage image = QImageReader(fileName, format).read();
- if (!image.isNull()) {
- operator=(image);
- return true;
- }
- return false;
+ operator=(image);
+ return !isNull();
}
/*!
@@ -4384,11 +4379,8 @@ bool QImage::load(const QString &fileName, const char* format)
bool QImage::load(QIODevice* device, const char* format)
{
QImage image = QImageReader(device, format).read();
- if(!image.isNull()) {
- operator=(image);
- return true;
- }
- return false;
+ operator=(image);
+ return !isNull();
}
/*!
@@ -4396,7 +4388,7 @@ bool QImage::load(QIODevice* device, const char* format)
Loads an image from the first \a len bytes of the given binary \a
data. Returns true if the image was successfully loaded; otherwise
- returns false.
+ invalidates the image and returns false.
The loader attempts to read the image using the specified \a format, e.g.,
PNG or JPG. If \a format is not specified (which is the default), the
@@ -4408,11 +4400,8 @@ bool QImage::load(QIODevice* device, const char* format)
bool QImage::loadFromData(const uchar *data, int len, const char *format)
{
QImage image = fromData(data, len, format);
- if (!image.isNull()) {
- operator=(image);
- return true;
- }
- return false;
+ operator=(image);
+ return !isNull();
}
/*!
diff --git a/src/gui/image/qpicture.cpp b/src/gui/image/qpicture.cpp
index 464e31a3e2..f75c6e40df 100644
--- a/src/gui/image/qpicture.cpp
+++ b/src/gui/image/qpicture.cpp
@@ -246,7 +246,7 @@ void QPicture::setData(const char* data, uint size)
/*!
Loads a picture from the file specified by \a fileName and returns
- true if successful; otherwise returns false.
+ true if successful; otherwise invalidates the picture and returns false.
Please note that the \a format parameter has been deprecated and
will have no effect.
@@ -257,8 +257,10 @@ void QPicture::setData(const char* data, uint size)
bool QPicture::load(const QString &fileName, const char *format)
{
QFile f(fileName);
- if (!f.open(QIODevice::ReadOnly))
+ if (!f.open(QIODevice::ReadOnly)) {
+ operator=(QPicture());
return false;
+ }
return load(&f, format);
}
@@ -273,18 +275,14 @@ bool QPicture::load(QIODevice *dev, const char *format)
if(format) {
#ifndef QT_NO_PICTUREIO
QPictureIO io(dev, format);
- bool result = io.read();
- if (result) {
+ if (io.read()) {
operator=(io.picture());
-
- } else if (format)
-#else
- bool result = false;
-#endif
- {
- qWarning("QPicture::load: No such picture format: %s", format);
+ return true;
}
- return result;
+#endif
+ qWarning("QPicture::load: No such picture format: %s", format);
+ operator=(QPicture());
+ return false;
}
detach();
diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp
index 19497af391..113369fd75 100644
--- a/src/gui/image/qpixmap.cpp
+++ b/src/gui/image/qpixmap.cpp
@@ -684,8 +684,8 @@ QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode)
/*!
Loads a pixmap from the file with the given \a fileName. Returns
- true if the pixmap was successfully loaded; otherwise returns
- false.
+ true if the pixmap was successfully loaded; otherwise invalidates
+ the pixmap and returns false.
The loader attempts to read the pixmap using the specified \a
format. If the \a format is not specified (which is the default),
@@ -711,8 +711,10 @@ QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode)
bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
{
- if (fileName.isEmpty())
+ if (fileName.isEmpty()) {
+ data.reset();
return false;
+ }
QFileInfo info(fileName);
QString key = QLatin1String("qt_pixmap")
@@ -723,19 +725,23 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers
// Note: If no extension is provided, we try to match the
// file against known plugin extensions
- if (!info.completeSuffix().isEmpty() && !info.exists())
+ if (!info.completeSuffix().isEmpty() && !info.exists()) {
+ data.reset();
return false;
+ }
- if (QPixmapCache::find(key, *this))
+ if (QPixmapCache::find(key, this))
return true;
- QScopedPointer<QPlatformPixmap> tmp(QPlatformPixmap::create(0, 0, data ? data->type : QPlatformPixmap::PixmapType));
- if (tmp->fromFile(fileName, format, flags)) {
- data = tmp.take();
+ if (!data)
+ data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
+
+ if (data->fromFile(fileName, format, flags)) {
QPixmapCache::insert(key, *this);
return true;
}
+ data.reset();
return false;
}
@@ -744,7 +750,7 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers
Loads a pixmap from the \a len first bytes of the given binary \a
data. Returns true if the pixmap was loaded successfully;
- otherwise returns false.
+ otherwise invalidates the pixmap and returns false.
The loader attempts to read the pixmap using the specified \a
format. If the \a format is not specified (which is the default),
@@ -760,13 +766,19 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers
bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
{
- if (len == 0 || buf == 0)
+ if (len == 0 || buf == 0) {
+ data.reset();
return false;
+ }
if (!data)
data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
- return data->fromData(buf, len, format, flags);
+ if (data->fromData(buf, len, format, flags))
+ return true;
+
+ data.reset();
+ return false;
}
/*!