Skip to content

Commit 7b0bec7

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. (cherry-picked and adapted from master 85128c5) Fixes #14844
1 parent 04ec683 commit 7b0bec7

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
@@ -927,6 +927,20 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString& theProj4String
927927
OSRDestroySpatialReference( mCRS );
928928
mCRS = OSRNewSpatialReference( nullptr );
929929
mIsValidFlag = OSRImportFromProj4( mCRS, theProj4String.trimmed().toLatin1().constData() ) == OGRERR_NONE;
930+
// OSRImportFromProj4() may accept strings that are not valid proj.4 strings,
931+
// e.g if they lack a +ellps parameter, it will automatically add +ellps=WGS84, but as
932+
// we use the original mProj4 with QgsCoordinateTransform, it will fail to initialize
933+
// so better detect it now.
934+
projPJ theProj = pj_init_plus( theProj4String.trimmed().toLatin1().constData() );
935+
if ( !theProj )
936+
{
937+
QgsDebugMsg( "proj.4 string rejected by pj_init_plus()" );
938+
mIsValidFlag = false;
939+
}
940+
else
941+
{
942+
pj_free( theProj );
943+
}
930944
mWkt.clear();
931945
setMapUnits();
932946

‎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()
@@ -464,5 +465,11 @@ void TestQgsCoordinateReferenceSystem::debugPrint(
464465
}
465466
}
466467

468+
void TestQgsCoordinateReferenceSystem::createFromProj4Invalid()
469+
{
470+
QgsCoordinateReferenceSystem myCrs;
471+
QVERIFY( !myCrs.createFromProj4( "+proj=longlat +no_defs" ) );
472+
}
473+
467474
QTEST_MAIN( TestQgsCoordinateReferenceSystem )
468475
#include "testqgscoordinatereferencesystem.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.