Skip to content

Commit

Permalink
Fix crash in geometry serialisation when no geometry set
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 24, 2015
1 parent 2acff9b commit 3c0fe13
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -2246,6 +2246,12 @@ QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry )
{ {
QByteArray byteArray; QByteArray byteArray;
in >> byteArray; in >> byteArray;
if ( byteArray.isEmpty() )
{
geometry.setGeometry( 0 );
return in;
}

char *data = new char[byteArray.size()]; char *data = new char[byteArray.size()];
memcpy( data, byteArray.data(), byteArray.size() ); memcpy( data, byteArray.data(), byteArray.size() );
geometry.fromWkb(( unsigned char* )data, byteArray.size() ); geometry.fromWkb(( unsigned char* )data, byteArray.size() );
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsfeature.cpp
Expand Up @@ -419,7 +419,7 @@ void TestQgsFeature::dataStream()
originalFeature.setGeometry( new QgsGeometry( *mGeometry.data() ) ); originalFeature.setGeometry( new QgsGeometry( *mGeometry.data() ) );


QByteArray ba; QByteArray ba;
QDataStream ds( &ba, QIODevice::ReadWrite );; QDataStream ds( &ba, QIODevice::ReadWrite );
ds << originalFeature; ds << originalFeature;


QgsFeature resultFeature; QgsFeature resultFeature;
Expand All @@ -434,7 +434,7 @@ void TestQgsFeature::dataStream()
//also test with feature without geometry //also test with feature without geometry
originalFeature.setGeometry( new QgsGeometry() ); originalFeature.setGeometry( new QgsGeometry() );
QByteArray ba2; QByteArray ba2;
QDataStream ds2( &ba2, QIODevice::ReadWrite );; QDataStream ds2( &ba2, QIODevice::ReadWrite );
ds2 << originalFeature; ds2 << originalFeature;


ds2.device()->seek( 0 ); ds2.device()->seek( 0 );
Expand Down
30 changes: 30 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -80,6 +80,8 @@ class TestQgsGeometry : public QObject
void bufferCheck(); void bufferCheck();
void smoothCheck(); void smoothCheck();


void dataStream();

private: private:
/** A helper method to do a render check to see if the geometry op is as expected */ /** A helper method to do a render check to see if the geometry op is as expected */
bool renderCheck( QString theTestName, QString theComment = "", int mismatchCount = 0 ); bool renderCheck( QString theTestName, QString theComment = "", int mismatchCount = 0 );
Expand Down Expand Up @@ -706,6 +708,34 @@ void TestQgsGeometry::smoothCheck()
QVERIFY( QgsGeometry::compare( multipoly, expectedMultiPoly ) ); QVERIFY( QgsGeometry::compare( multipoly, expectedMultiPoly ) );
} }


void TestQgsGeometry::dataStream()
{
QString wkt = "Point (40 50)";
QScopedPointer<QgsGeometry> geom( QgsGeometry::fromWkt( wkt ) );

QByteArray ba;
QDataStream ds( &ba, QIODevice::ReadWrite );
ds << *geom;

QgsGeometry resultGeometry;
ds.device()->seek( 0 );
ds >> resultGeometry;

QCOMPARE( geom->geometry()->asWkt(), resultGeometry.geometry()->asWkt( ) );

//also test with geometry without data
QScopedPointer<QgsGeometry> emptyGeom( new QgsGeometry() );

QByteArray ba2;
QDataStream ds2( &ba2, QIODevice::ReadWrite );
ds2 << emptyGeom;

ds2.device()->seek( 0 );
ds2 >> resultGeometry;

QVERIFY( resultGeometry.isEmpty() );
}

bool TestQgsGeometry::renderCheck( QString theTestName, QString theComment, int mismatchCount ) bool TestQgsGeometry::renderCheck( QString theTestName, QString theComment, int mismatchCount )
{ {
mReport += "<h2>" + theTestName + "</h2>\n"; mReport += "<h2>" + theTestName + "</h2>\n";
Expand Down

0 comments on commit 3c0fe13

Please sign in to comment.