Skip to content

Commit

Permalink
Merge pull request OSGeo#8788 from rouault/fix_8743
Browse files Browse the repository at this point in the history
KEA: Create(): error out if passing a /vsi file. avoids crashes (fixes OSGeo#8743)
  • Loading branch information
rouault authored Nov 22, 2023
2 parents 1bd5852 + 7c37cda commit a44741d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions autotest/gdrivers/kea.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,14 @@ def test_kea_15(tmp_path, tmp_vsimem):
ds = gdal.Open(tmp_vsimem / "foo.kea")
assert ds.GetDriver().ShortName == "KEA"
ds = None


###############################################################################
# Test /vsi functionality on writing (does not work)


@gdaltest.enable_exceptions()
def test_kea_create_vsimem(tmp_vsimem):

with pytest.raises(Exception):
gdaltest.kea_driver.Create(tmp_vsimem / "vsitest.kea", 1, 1)
30 changes: 30 additions & 0 deletions frmts/kea/keadataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "keacopy.h"
#include "keadrivercore.h"
#include "../frmts/hdf5/hdf5vfl.h"
#include "cpl_vsi_virtual.h"

/************************************************************************/
/* KEADatasetDriverUnload() */
Expand Down Expand Up @@ -180,6 +181,14 @@ GDALDataset *KEADataset::Open(GDALOpenInfo *poOpenInfo)
poOpenInfo->pszFilename, e.what());
return nullptr;
}
catch (...)
{
// was a problem - can't be a valid file
CPLError(CE_Failure, CPLE_OpenFailed,
"Attempt to open file `%s' failed. Error: unknown\n",
poOpenInfo->pszFilename);
return nullptr;
}
}
else
{
Expand All @@ -203,6 +212,20 @@ H5::H5File *KEADataset::CreateLL(const char *pszFilename, int nXSize,
pszFilename);
return nullptr;
}

// This helps avoiding issues with H5File handles in a bad state, that
// may cause crashes at process termination
// Cf https://github.com/OSGeo/gdal/issues/8743
if (VSIFileManager::GetHandler(pszFilename) !=
VSIFileManager::GetHandler(""))
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Attempt to create file `%s' failed. /vsi file systems not "
"supported\n",
pszFilename);
return nullptr;
}

// process any creation options in papszParamList
// default value
unsigned int nimageblockSize = kealib::KEA_IMAGE_CHUNK_SIZE;
Expand Down Expand Up @@ -276,6 +299,13 @@ H5::H5File *KEADataset::CreateLL(const char *pszFilename, int nXSize,
e.what());
return nullptr;
}
catch (...)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Attempt to create file `%s' failed. Error: unknown\n",
pszFilename);
return nullptr;
}
}

// static function- pointer set in driver
Expand Down

0 comments on commit a44741d

Please sign in to comment.