Skip to content
Permalink
Browse files

QgsVectorFileWriter: workaround a bug in GDAL where appending to an e…

…xisting BNA crashes it (fixes #18563)

Upstream fixes done in GDAL as well to fix the crash itself:
GDAL master: OSGeo/gdal@a36939a
GDAL 2.3 branch: OSGeo/gdal@f9af14b
  • Loading branch information
rouault committed Jun 1, 2018
1 parent bd65fc6 commit 43f796e78ef6faa6240c70a84ffd5acfc2c326a6
Showing with 27 additions and 4 deletions.
  1. +27 −4 src/core/qgsvectorfilewriter.cpp
@@ -52,6 +52,29 @@
#include <cpl_string.h>
#include <gdal.h>

// Thin wrapper around OGROpen() to workaround a bug in GDAL < 2.3.1
// where a existing BNA file is wrongly reported to be openable in update mode
// but attempting to add features in it crashes the BNA driver.
static OGRDataSourceH myOGROpen( const char *pszName, int bUpdate, OGRSFDriverH *phDriver )
{
OGRSFDriverH hDriver = nullptr;
OGRDataSourceH hDS = OGROpen( pszName, bUpdate, &hDriver );
if ( hDS && bUpdate )
{
QString drvName = OGR_Dr_GetName( hDriver );
if ( drvName == "BNA" )
{
OGR_DS_Destroy( hDS );
if ( phDriver )
*phDriver = nullptr;
return nullptr;
}
}
if ( phDriver )
*phDriver = hDriver;
return hDS;
}

QgsField QgsVectorFileWriter::FieldValueConverter::fieldDefinition( const QgsField &field )
{
return field;
@@ -284,7 +307,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
if ( action == CreateOrOverwriteFile )
mDS.reset( OGR_Dr_CreateDataSource( poDriver, vectorFileName.toUtf8().constData(), options ) );
else
mDS.reset( OGROpen( vectorFileName.toUtf8().constData(), TRUE, nullptr ) );
mDS.reset( myOGROpen( vectorFileName.toUtf8().constData(), TRUE, nullptr ) );

if ( options )
{
@@ -3372,7 +3395,7 @@ QStringList QgsVectorFileWriter::concatenateOptions( const QMap<QString, QgsVect
QgsVectorFileWriter::EditionCapabilities QgsVectorFileWriter::editionCapabilities( const QString &datasetName )
{
OGRSFDriverH hDriver = nullptr;
gdal::ogr_datasource_unique_ptr hDS( OGROpen( datasetName.toUtf8().constData(), TRUE, &hDriver ) );
gdal::ogr_datasource_unique_ptr hDS( myOGROpen( datasetName.toUtf8().constData(), TRUE, &hDriver ) );
if ( !hDS )
return nullptr;
QString drvName = OGR_Dr_GetName( hDriver );
@@ -3412,7 +3435,7 @@ bool QgsVectorFileWriter::targetLayerExists( const QString &datasetName,
const QString &layerNameIn )
{
OGRSFDriverH hDriver = nullptr;
gdal::ogr_datasource_unique_ptr hDS( OGROpen( datasetName.toUtf8().constData(), TRUE, &hDriver ) );
gdal::ogr_datasource_unique_ptr hDS( myOGROpen( datasetName.toUtf8().constData(), TRUE, &hDriver ) );
if ( !hDS )
return false;

@@ -3430,7 +3453,7 @@ bool QgsVectorFileWriter::areThereNewFieldsToCreate( const QString &datasetName,
const QgsAttributeList &attributes )
{
OGRSFDriverH hDriver = nullptr;
gdal::ogr_datasource_unique_ptr hDS( OGROpen( datasetName.toUtf8().constData(), TRUE, &hDriver ) );
gdal::ogr_datasource_unique_ptr hDS( myOGROpen( datasetName.toUtf8().constData(), TRUE, &hDriver ) );
if ( !hDS )
return false;
OGRLayerH hLayer = OGR_DS_GetLayerByName( hDS.get(), layerName.toUtf8().constData() );

0 comments on commit 43f796e

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