Skip to content
Permalink
Browse files

Fix crash in geometry serialisation when no geometry set

  • Loading branch information
nyalldawson committed Aug 24, 2015
1 parent 2acff9b commit 3c0fe13207a3e54b7e2c0526dd8b82f6428c495a
Showing with 38 additions and 2 deletions.
  1. +6 −0 src/core/geometry/qgsgeometry.cpp
  2. +2 −2 tests/src/core/testqgsfeature.cpp
  3. +30 −0 tests/src/core/testqgsgeometry.cpp
@@ -2246,6 +2246,12 @@ QDataStream& operator>>( QDataStream& in, QgsGeometry& geometry )
{
QByteArray byteArray;
in >> byteArray;
if ( byteArray.isEmpty() )
{
geometry.setGeometry( 0 );
return in;
}

char *data = new char[byteArray.size()];
memcpy( data, byteArray.data(), byteArray.size() );
geometry.fromWkb(( unsigned char* )data, byteArray.size() );
@@ -419,7 +419,7 @@ void TestQgsFeature::dataStream()
originalFeature.setGeometry( new QgsGeometry( *mGeometry.data() ) );

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

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

ds2.device()->seek( 0 );
@@ -80,6 +80,8 @@ class TestQgsGeometry : public QObject
void bufferCheck();
void smoothCheck();

void dataStream();

private:
/** 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 );
@@ -706,6 +708,34 @@ void TestQgsGeometry::smoothCheck()
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 )
{
mReport += "<h2>" + theTestName + "</h2>\n";

0 comments on commit 3c0fe13

Please sign in to comment.
You can’t perform that action at this time.