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.

Fixes #14844
  • Loading branch information
rouault committed Jun 20, 2016
1 parent 8783941 commit 85128c5
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
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,20 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString& theProj4String
OSRDestroySpatialReference( d->mCRS );
d->mCRS = OSRNewSpatialReference( nullptr );
d->mIsValid = OSRImportFromProj4( d->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()" );
d->mIsValid = false;
}
else
{
pj_free( theProj );
}
d->mWkt.clear();
setMapUnits();

Expand Down
7 changes: 7 additions & 0 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Original file line number Diff line number Diff line change
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 @@ -488,5 +489,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 85128c5

Please sign in to comment.