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.

Fixes #14844
  • Loading branch information
rouault committed Jun 19, 2016
1 parent 8783941 commit 85128c54191cfedeaee04ca9c4ac0341ab8f5088
Showing with 21 additions and 0 deletions.
  1. +14 −0 src/core/qgscoordinatereferencesystem.cpp
  2. +7 −0 tests/src/core/testqgscoordinatereferencesystem.cpp
@@ -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();

@@ -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()
@@ -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.
You can’t perform that action at this time.