Skip to content

Commit

Permalink
don't crash when measuring faulty geometry (fixes partially #7991)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jun 30, 2013
1 parent 608526c commit fe9b8d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/core/qgsdistancearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ double QgsDistanceArea::measure( QgsGeometry* geometry )
for ( i = 0; i < count; i++ )
{
ptr = measurePolygon( ptr, &res, 0, hasZptr );
if ( !ptr )
{
QgsDebugMsg( "measurePolygon returned 0" );
break;
}
resTotal += res;
}
QgsDebugMsg( "returning " + QString::number( resTotal ) );
Expand All @@ -330,7 +335,7 @@ double QgsDistanceArea::measurePerimeter( QgsGeometry* geometry )

const unsigned char* ptr;
unsigned int wkbType;
double res, resTotal = 0;
double res = 0.0, resTotal = 0.0;
int count, i;

memcpy( &wkbType, ( wkb + 1 ), sizeof( wkbType ) );
Expand Down Expand Up @@ -361,6 +366,11 @@ double QgsDistanceArea::measurePerimeter( QgsGeometry* geometry )
for ( i = 0; i < count; i++ )
{
ptr = measurePolygon( ptr, 0, &res, hasZptr );
if ( !ptr )
{
QgsDebugMsg( "measurePolygon returned 0" );
break;
}
resTotal += res;
}
QgsDebugMsg( "returning " + QString::number( resTotal ) );
Expand Down Expand Up @@ -483,11 +493,20 @@ double QgsDistanceArea::measureLine( const QgsPoint& p1, const QgsPoint& p2 )

const unsigned char* QgsDistanceArea::measurePolygon( const unsigned char* feature, double* area, double* perimeter, bool hasZptr )
{
if ( !feature )
{
QgsDebugMsg( "no feature to measure" );
return 0;
}

// get number of rings in the polygon
unsigned int numRings = *(( int* )( feature + 1 + sizeof( int ) ) );

if ( numRings == 0 )
{
QgsDebugMsg( "no rings to measure" );
return 0;
}

// Set pointer to the first ring
const unsigned char* ptr = feature + 1 + 2 * sizeof( int );
Expand Down Expand Up @@ -571,7 +590,6 @@ const unsigned char* QgsDistanceArea::measurePolygon( const unsigned char* featu

double QgsDistanceArea::measurePolygon( const QList<QgsPoint>& points )
{

try
{
if ( mEllipsoidalMode && ( mEllipsoid != GEO_NONE ) )
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsgeometryvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,18 @@ void QgsGeometryValidator::run()

for ( int i = 0; !mStop && i < mp.size(); i++ )
{
if ( mp[i].isEmpty() )
{
emit errorFound( QgsGeometry::Error( QObject::tr( "polygon %1 has no rings" ).arg( i ) ) );
mErrorCount++;
continue;
}

for ( int j = i + 1; !mStop && j < mp.size(); j++ )
{
if ( mp[j].isEmpty() )
continue;

if ( ringInRing( mp[i][0], mp[j][0] ) )
{
emit errorFound( QgsGeometry::Error( QObject::tr( "polygon %1 inside polygon %2" ).arg( i ).arg( j ) ) );
Expand Down

0 comments on commit fe9b8d4

Please sign in to comment.