Skip to content

Commit

Permalink
Merge pull request #7141 from rouault/fix_18563
Browse files Browse the repository at this point in the history
QgsVectorFileWriter: workaround a bug in GDAL where appending to an existing BNA crashes it (fixes #18563)
  • Loading branch information
rouault committed Jun 1, 2018
2 parents e4d5899 + 43f796e commit 3b29102
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;

Expand All @@ -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() );
Expand Down

0 comments on commit 3b29102

Please sign in to comment.