Skip to content

Commit

Permalink
VRTDerivedRasterBand: avoid potential initialization order fiasco (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 19, 2024
1 parent e7f14b7 commit ab4f12d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion frmts/vrt/vrtdataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ class CPL_DLL VRTDerivedRasterBand CPL_NON_FINAL : public VRTSourcedRasterBand
GDALDerivedPixelFuncWithArgs pfnPixelFunc,
const char *pszMetadata);

static std::pair<PixelFunc, CPLString> *
static const std::pair<PixelFunc, std::string> *
GetPixelFunction(const char *pszFuncNameIn);

void SetPixelFunctionName(const char *pszFuncNameIn);
Expand Down
33 changes: 22 additions & 11 deletions frmts/vrt/vrtderivedrasterband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ using namespace GDALPy;
#define GDAL_VRT_ENABLE_PYTHON_DEFAULT "TRUSTED_MODULES"
#endif

static std::map<CPLString,
std::pair<VRTDerivedRasterBand::PixelFunc, CPLString>>
osMapPixelFunction;

/* Flags for getting buffers */
#define PyBUF_WRITABLE 0x0001
#define PyBUF_FORMAT 0x0004
Expand Down Expand Up @@ -221,6 +217,20 @@ void VRTDerivedRasterBand::Cleanup()
{
}

/************************************************************************/
/* GetGlobalMapPixelFunction() */
/************************************************************************/

static std::map<std::string,
std::pair<VRTDerivedRasterBand::PixelFunc, std::string>> &
GetGlobalMapPixelFunction()
{
static std::map<std::string,
std::pair<VRTDerivedRasterBand::PixelFunc, std::string>>
gosMapPixelFunction;
return gosMapPixelFunction;
}

/************************************************************************/
/* AddPixelFunction() */
/************************************************************************/
Expand Down Expand Up @@ -251,7 +261,7 @@ CPLErr CPL_STDCALL GDALAddDerivedBandPixelFunc(
return CE_None;
}

osMapPixelFunction[pszName] = {
GetGlobalMapPixelFunction()[pszName] = {
[pfnNewFunction](void **papoSources, int nSources, void *pData,
int nBufXSize, int nBufYSize, GDALDataType eSrcType,
GDALDataType eBufType, int nPixelSpace, int nLineSpace,
Expand Down Expand Up @@ -294,8 +304,8 @@ CPLErr CPL_STDCALL GDALAddDerivedBandPixelFuncWithArgs(
return CE_None;
}

osMapPixelFunction[pszName] = {pfnNewFunction,
pszMetadata != nullptr ? pszMetadata : ""};
GetGlobalMapPixelFunction()[pszName] = {
pfnNewFunction, pszMetadata != nullptr ? pszMetadata : ""};

return CE_None;
}
Expand Down Expand Up @@ -343,17 +353,18 @@ CPLErr VRTDerivedRasterBand::AddPixelFunction(
* @return A derived band pixel function, or NULL if none have been
* registered for pszFuncName.
*/
std::pair<VRTDerivedRasterBand::PixelFunc, CPLString> *
const std::pair<VRTDerivedRasterBand::PixelFunc, std::string> *
VRTDerivedRasterBand::GetPixelFunction(const char *pszFuncNameIn)
{
if (pszFuncNameIn == nullptr || pszFuncNameIn[0] == '\0')
{
return nullptr;
}

auto oIter = osMapPixelFunction.find(pszFuncNameIn);
const auto &oMapPixelFunction = GetGlobalMapPixelFunction();
const auto oIter = oMapPixelFunction.find(pszFuncNameIn);

if (oIter == osMapPixelFunction.end())
if (oIter == oMapPixelFunction.end())
return nullptr;

return &(oIter->second);
Expand Down Expand Up @@ -935,7 +946,7 @@ CPLErr VRTDerivedRasterBand::IRasterIO(
}

/* ---- Get pixel function for band ---- */
std::pair<PixelFunc, CPLString> *poPixelFunc = nullptr;
const std::pair<PixelFunc, std::string> *poPixelFunc = nullptr;
std::vector<std::pair<CPLString, CPLString>> oAdditionalArgs;

if (EQUAL(m_poPrivate->m_osLanguage, "C"))
Expand Down

0 comments on commit ab4f12d

Please sign in to comment.