Skip to content
Permalink
Browse files

QgsCoordinateReferenceSystem::setProj4String(): harden validation

OSRImportFromProj4() may accept strings that are not valid proj.4 strings,
e.g if they lack a +ellps parameter, it will automatically add +ellps=WGS84, but as
we use the original mProj4 with QgsCoordinateTransform, it will fail to initialize
so better detect it now.

(cherry-picked and adapted from master 85128c5)

Fixes #14844
  • Loading branch information
rouault committed Jun 20, 2016
1 parent 04ec683 commit 7b0bec7be8fcfa7dd4f8635cfe8e656f2c0fd6e9
Showing with 21 additions and 0 deletions.
  1. +14 −0 src/core/qgscoordinatereferencesystem.cpp
  2. +7 −0 tests/src/core/testqgscoordinatereferencesystem.cpp
@@ -927,6 +927,20 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString& theProj4String
OSRDestroySpatialReference( mCRS );
mCRS = OSRNewSpatialReference( nullptr );
mIsValidFlag = OSRImportFromProj4( mCRS, theProj4String.trimmed().toLatin1().constData() ) == OGRERR_NONE;
// OSRImportFromProj4() may accept strings that are not valid proj.4 strings,
// e.g if they lack a +ellps parameter, it will automatically add +ellps=WGS84, but as
// we use the original mProj4 with QgsCoordinateTransform, it will fail to initialize
// so better detect it now.
projPJ theProj = pj_init_plus( theProj4String.trimmed().toLatin1().constData() );
if ( !theProj )
{
QgsDebugMsg( "proj.4 string rejected by pj_init_plus()" );
mIsValidFlag = false;
}
else
{
pj_free( theProj );
}
mWkt.clear();
setMapUnits();

@@ -61,6 +61,7 @@ class TestQgsCoordinateReferenceSystem: public QObject
void mapUnits();
void setValidationHint();
void axisInverted();
void createFromProj4Invalid();
private:
void debugPrint( QgsCoordinateReferenceSystem &theCrs );
// these used by createFromESRIWkt()
@@ -464,5 +465,11 @@ void TestQgsCoordinateReferenceSystem::debugPrint(
}
}

void TestQgsCoordinateReferenceSystem::createFromProj4Invalid()
{
QgsCoordinateReferenceSystem myCrs;
QVERIFY( !myCrs.createFromProj4( "+proj=longlat +no_defs" ) );
}

QTEST_MAIN( TestQgsCoordinateReferenceSystem )
#include "testqgscoordinatereferencesystem.moc"

0 comments on commit 7b0bec7

Please sign in to comment.
You can’t perform that action at this time.