Skip to content

Commit bafda24

Browse files
committed
Correctly handle empty paths in QgsImageCache
1 parent 58219d6 commit bafda24

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/core/qgsimagecache.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ QgsImageCache::QgsImageCache( QObject *parent )
101101
connect( this, &QgsAbstractContentCacheBase::remoteContentFetched, this, &QgsImageCache::remoteImageFetched );
102102
}
103103

104-
QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache )
104+
QImage QgsImageCache::pathAsImage( const QString &f, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache )
105105
{
106+
const QString file = f.trimmed();
107+
108+
if ( file.isEmpty() )
109+
return QImage();
110+
106111
QMutexLocker locker( &mMutex );
107112

108113
fitsInCache = true;
@@ -141,6 +146,9 @@ QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const
141146

142147
QSize QgsImageCache::originalSize( const QString &path ) const
143148
{
149+
if ( path.isEmpty() )
150+
return QSize();
151+
144152
// direct read if path is a file -- maybe more efficient than going the bytearray route? (untested!)
145153
if ( QFile::exists( path ) )
146154
{

tests/src/core/testqgsimagecache.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class TestQgsImageCache : public QObject
5353
void size(); // check various size-specific handling
5454
void opacity(); // check non-opaque image rendering
5555
void base64();
56+
void empty();
5657

5758
};
5859

@@ -254,6 +255,22 @@ void TestQgsImageCache::base64()
254255
QCOMPARE( size.height(), 60 );
255256
}
256257

258+
void TestQgsImageCache::empty()
259+
{
260+
QgsImageCache cache;
261+
bool inCache = false;
262+
263+
QImage img = cache.pathAsImage( QString(), QSize( 200, 200 ), true, 1.0, inCache );
264+
QVERIFY( img.isNull() );
265+
266+
QVERIFY( !cache.originalSize( QString() ).isValid() );
267+
268+
img = cache.pathAsImage( QStringLiteral( " " ), QSize( 200, 200 ), true, 1.0, inCache );
269+
QVERIFY( img.isNull() );
270+
271+
QVERIFY( !cache.originalSize( QStringLiteral( " " ) ).isValid() );
272+
}
273+
257274
bool TestQgsImageCache::imageCheck( const QString &testName, QImage &image, int mismatchCount )
258275
{
259276
//draw background

0 commit comments

Comments
 (0)