From 3c0fe13207a3e54b7e2c0526dd8b82f6428c495a Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 24 Aug 2015 19:57:15 +1000 Subject: [PATCH] Fix crash in geometry serialisation when no geometry set --- src/core/geometry/qgsgeometry.cpp | 6 ++++++ tests/src/core/testqgsfeature.cpp | 4 ++-- tests/src/core/testqgsgeometry.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/core/geometry/qgsgeometry.cpp b/src/core/geometry/qgsgeometry.cpp index 085655315884..454c9aba80d7 100644 --- a/src/core/geometry/qgsgeometry.cpp +++ b/src/core/geometry/qgsgeometry.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() ); diff --git a/tests/src/core/testqgsfeature.cpp b/tests/src/core/testqgsfeature.cpp index 995214779ebb..5c2235670534 100644 --- a/tests/src/core/testqgsfeature.cpp +++ b/tests/src/core/testqgsfeature.cpp @@ -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 ); diff --git a/tests/src/core/testqgsgeometry.cpp b/tests/src/core/testqgsgeometry.cpp index 4dfeebe0bca1..37fc885e6587 100644 --- a/tests/src/core/testqgsgeometry.cpp +++ b/tests/src/core/testqgsgeometry.cpp @@ -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 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 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 += "

" + theTestName + "

\n";