From bc89bdb17003a148672068c8a42ec9f39a1183b6 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 10 Jan 2016 17:36:07 +1100 Subject: [PATCH] Fix coverity issues: - possible use after free - uninitialized member --- .../geometry_checker/utils/qgsfeaturepool.cpp | 27 +++++++++++-------- .../virtual/qgsvirtuallayersqlitemodule.cpp | 2 ++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/plugins/geometry_checker/utils/qgsfeaturepool.cpp b/src/plugins/geometry_checker/utils/qgsfeaturepool.cpp index d366d2cc1cf..86e1d7d8a5b 100644 --- a/src/plugins/geometry_checker/utils/qgsfeaturepool.cpp +++ b/src/plugins/geometry_checker/utils/qgsfeaturepool.cpp @@ -52,19 +52,24 @@ bool QgsFeaturePool::get( QgsFeatureId id , QgsFeature& feature ) { QMutexLocker lock( &mLayerMutex ); QgsFeature* pfeature = mFeatureCache.object( id ); - if ( !pfeature ) + if ( pfeature ) { - // Get new feature - pfeature = new QgsFeature(); - // TODO: avoid always querying all attributes (attribute values are needed when merging by attribute) - if ( !mLayer->getFeatures( QgsFeatureRequest( id ) ).nextFeature( *pfeature ) ) - { - delete pfeature; - return false; - } - mFeatureCache.insert( id, pfeature ); + //feature was cached + feature = *pfeature; } - feature = *pfeature; + + // Feature not in cache, retrieve from layer + pfeature = new QgsFeature(); + // TODO: avoid always querying all attributes (attribute values are needed when merging by attribute) + if ( !mLayer->getFeatures( QgsFeatureRequest( id ) ).nextFeature( *pfeature ) ) + { + delete pfeature; + return false; + } + //make a copy of pfeature into feature parameter + feature = QgsFeature( *pfeature ); + //ownership of pfeature is transferred to cache + mFeatureCache.insert( id, pfeature ); return true; } diff --git a/src/providers/virtual/qgsvirtuallayersqlitemodule.cpp b/src/providers/virtual/qgsvirtuallayersqlitemodule.cpp index 7158ef44833..975a01ee60d 100644 --- a/src/providers/virtual/qgsvirtuallayersqlitemodule.cpp +++ b/src/providers/virtual/qgsvirtuallayersqlitemodule.cpp @@ -105,6 +105,7 @@ struct VTable , mSlotToFunction( invalidateTable, this ) , mName( layer->name() ) , mPkColumn( -1 ) + , mCrs( -1 ) , mValid( true ) { if ( mLayer ) @@ -123,6 +124,7 @@ struct VTable , mName( name ) , mEncoding( encoding ) , mPkColumn( -1 ) + , mCrs( -1 ) , mValid( true ) { mProvider = static_cast( QgsProviderRegistry::instance()->provider( provider, source ) );