From 07adb1c113f64c238fdcf73cf78a97f6fa193137 Mon Sep 17 00:00:00 2001 From: Even Rouault <even.rouault@spatialys.com> Date: Sat, 11 Jan 2025 03:33:27 +0100 Subject: [PATCH] ExprPixelFunc(): fix memory leak in error code path --- frmts/vrt/pixelfunctions.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/frmts/vrt/pixelfunctions.cpp b/frmts/vrt/pixelfunctions.cpp index eb3fe25ef649..9f9bc580fd88 100644 --- a/frmts/vrt/pixelfunctions.cpp +++ b/frmts/vrt/pixelfunctions.cpp @@ -1675,14 +1675,16 @@ static CPLErr ExprPixelFunc(void **papoSources, int nSources, void *pData, &adfValuesForPixel[iSource++]); } } - CPLString osExpression(pszExpression); - if (osExpression.find("BANDS") != std::string::npos) + + if (strstr(pszExpression, "BANDS")) { poExpression->RegisterVector("BANDS", &adfValuesForPixel); } - double *padfResults = - static_cast<double *>(CPLMalloc(nXSize * sizeof(double))); + std::unique_ptr<double, VSIFreeReleaser> padfResults( + static_cast<double *>(VSI_MALLOC2_VERBOSE(nXSize, sizeof(double)))); + if (!padfResults) + return CE_Failure; /* ---- Set pixels ---- */ size_t ii = 0; @@ -1703,18 +1705,16 @@ static CPLErr ExprPixelFunc(void **papoSources, int nSources, void *pData, } else { - padfResults[iCol] = poExpression->Results()[0]; + padfResults.get()[iCol] = poExpression->Results()[0]; } } - GDALCopyWords(padfResults, GDT_Float64, sizeof(double), + GDALCopyWords(padfResults.get(), GDT_Float64, sizeof(double), static_cast<GByte *>(pData) + static_cast<GSpacing>(nLineSpace) * iLine, eBufType, nPixelSpace, nXSize); } - CPLFree(padfResults); - /* ---- Return success ---- */ return CE_None; } // ExprPixelFunc