Skip to content

Commit 2861cf1

Browse files
committed
QgsGdalProvider::buildPyramids(): do not crash if an invalid option is provided
1 parent a8be64e commit 2861cf1

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/providers/gdal/qgsgdalprovider.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -1598,13 +1598,20 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> & theRaste
15981598
Q_FOREACH ( const QString& option, theConfigOptions )
15991599
{
16001600
QStringList opt = option.split( '=' );
1601-
QByteArray key = opt[0].toLocal8Bit();
1602-
QByteArray value = opt[1].toLocal8Bit();
1603-
// save previous value
1604-
myConfigOptionsOld[ opt[0] ] = QString( CPLGetConfigOption( key.data(), nullptr ) );
1605-
// set temp. value
1606-
CPLSetConfigOption( key.data(), value.data() );
1607-
QgsDebugMsg( QString( "set option %1=%2" ).arg( key.data(), value.data() ) );
1601+
if ( opt.size() == 2 )
1602+
{
1603+
QByteArray key = opt[0].toLocal8Bit();
1604+
QByteArray value = opt[1].toLocal8Bit();
1605+
// save previous value
1606+
myConfigOptionsOld[ opt[0] ] = QString( CPLGetConfigOption( key.data(), nullptr ) );
1607+
// set temp. value
1608+
CPLSetConfigOption( key.data(), value.data() );
1609+
QgsDebugMsg( QString( "set option %1=%2" ).arg( key.data(), value.data() ) );
1610+
}
1611+
else
1612+
{
1613+
QgsDebugMsg( QString( "invalid pyramid option: %1" ).arg( option ) );
1614+
}
16081615
}
16091616
}
16101617

tests/src/core/testqgsrasterlayer.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QDesktopServices>
2525

2626
#include "cpl_conv.h"
27+
#include "gdal.h"
2728

2829
//qgis includes...
2930
#include <qgsrasterlayer.h>
@@ -512,6 +513,7 @@ void TestQgsRasterLayer::buildExternalOverviews()
512513
QString myResult =
513514
mypLayer->dataProvider()->buildPyramids( myPyramidList, "NEAREST", myFormatFlag );
514515
qDebug( "%s", myResult.toLocal8Bit().constData() );
516+
QVERIFY( myResult.isEmpty() );
515517
//
516518
// Lets verify we have pyramids now...
517519
//
@@ -526,8 +528,42 @@ void TestQgsRasterLayer::buildExternalOverviews()
526528
// And that they were indeed in an external file...
527529
//
528530
QVERIFY( QFile::exists( myTempPath + "landsat.tif.ovr" ) );
531+
532+
//cleanup
533+
delete mypLayer;
534+
535+
QFile::remove( myTempPath + "landsat.tif.ovr" );
536+
mypLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
537+
myRasterFileInfo.completeBaseName() );
538+
myPyramidList = mypLayer->dataProvider()->buildPyramidList();
539+
for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
540+
{
541+
//mark to be pyramided
542+
myPyramidList[myCounterInt].build = true;
543+
}
544+
545+
// Test with options
546+
QStringList optionList;
547+
optionList << "COMPRESS_OVERVIEW=DEFLATE";
548+
optionList << "invalid";
549+
550+
myResult =
551+
mypLayer->dataProvider()->buildPyramids( myPyramidList, "NEAREST", myFormatFlag, optionList );
552+
qDebug( "%s", myResult.toLocal8Bit().constData() );
553+
QVERIFY( myResult.isEmpty() );
554+
QVERIFY( QFile::exists( myTempPath + "landsat.tif.ovr" ) );
555+
529556
//cleanup
530557
delete mypLayer;
558+
559+
// Check that the overview is Deflate compressed
560+
QString ovrFilename( myTempPath + "landsat.tif.ovr" );
561+
GDALDatasetH hDS = GDALOpen( ovrFilename.toLocal8Bit().constData(), GA_ReadOnly );
562+
QVERIFY( hDS );
563+
const char* pszCompression = GDALGetMetadataItem( hDS, "COMPRESSION", "IMAGE_STRUCTURE" );
564+
QVERIFY( pszCompression && EQUAL( pszCompression, "DEFLATE" ) );
565+
GDALClose( hDS );
566+
531567
mReport += "<h2>Check Overviews</h2>\n";
532568
mReport += "<p>Passed</p>";
533569
}

0 commit comments

Comments
 (0)