Skip to content

Commit

Permalink
Merge pull request #8791 from OSGeo/backport-8788-to-release/3.8
Browse files Browse the repository at this point in the history
[Backport release/3.8] KEA: Create(): error out if passing a /vsi file. avoids crashes (fixes #8743)
  • Loading branch information
rouault authored Nov 22, 2023
2 parents 8e80174 + 733844a commit 6b520a0
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 @@ -31,6 +31,7 @@
#include "keaband.h"
#include "keacopy.h"
#include "../frmts/hdf5/hdf5vfl.h"
#include "cpl_vsi_virtual.h"

/************************************************************************/
/* KEADatasetDriverUnload() */
Expand Down Expand Up @@ -179,6 +180,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 Down Expand Up @@ -230,6 +239,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 @@ -303,6 +326,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 6b520a0

Please sign in to comment.