Skip to content

Commit

Permalink
QgsCoordinateReferenceSystem::setProj4String(): harden validation
Browse files Browse the repository at this point in the history
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 7b0bec7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -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.