Skip to content

Commit 85128c5

Browse files
committedJun 20, 2016
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
1 parent 8783941 commit 85128c5

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,20 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString& theProj4String
899899
OSRDestroySpatialReference( d->mCRS );
900900
d->mCRS = OSRNewSpatialReference( nullptr );
901901
d->mIsValid = OSRImportFromProj4( d->mCRS, theProj4String.trimmed().toLatin1().constData() ) == OGRERR_NONE;
902+
// OSRImportFromProj4() may accept strings that are not valid proj.4 strings,
903+
// e.g if they lack a +ellps parameter, it will automatically add +ellps=WGS84, but as
904+
// we use the original mProj4 with QgsCoordinateTransform, it will fail to initialize
905+
// so better detect it now.
906+
projPJ theProj = pj_init_plus( theProj4String.trimmed().toLatin1().constData() );
907+
if ( !theProj )
908+
{
909+
QgsDebugMsg( "proj.4 string rejected by pj_init_plus()" );
910+
d->mIsValid = false;
911+
}
912+
else
913+
{
914+
pj_free( theProj );
915+
}
902916
d->mWkt.clear();
903917
setMapUnits();
904918

‎tests/src/core/testqgscoordinatereferencesystem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class TestQgsCoordinateReferenceSystem: public QObject
6161
void mapUnits();
6262
void setValidationHint();
6363
void axisInverted();
64+
void createFromProj4Invalid();
6465
private:
6566
void debugPrint( QgsCoordinateReferenceSystem &theCrs );
6667
// these used by createFromESRIWkt()
@@ -488,5 +489,11 @@ void TestQgsCoordinateReferenceSystem::debugPrint(
488489
}
489490
}
490491

492+
void TestQgsCoordinateReferenceSystem::createFromProj4Invalid()
493+
{
494+
QgsCoordinateReferenceSystem myCrs;
495+
QVERIFY( !myCrs.createFromProj4( "+proj=longlat +no_defs" ) );
496+
}
497+
491498
QTEST_MAIN( TestQgsCoordinateReferenceSystem )
492499
#include "testqgscoordinatereferencesystem.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.