Skip to content

Commit

Permalink
Merge pull request #11793 from dbaston/vrt-muparser-fix
Browse files Browse the repository at this point in the history
muparser: avoid uncaught exception on invalid variable name
  • Loading branch information
rouault authored Feb 5, 2025
2 parents 28c687a + a8b75d0 commit f4ae692
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions autotest/gdrivers/vrtderived.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,13 @@ def test_vrt_pixelfn_expression(
"exceeds maximum of 100000 set by GDAL_EXPRTK_MAX_EXPRESSION_LENGTH",
id="expression is too long",
),
pytest.param(
"B[1]",
[("B[1]", 3)],
"muparser",
"Invalid variable name",
id="invalid variable name",
),
],
)
def test_vrt_pixelfn_expression_invalid(
Expand Down
19 changes: 17 additions & 2 deletions frmts/vrt/vrtexpression_muparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,31 @@ class MuParserExpression::Impl
public:
explicit Impl(std::string_view osExpression)
: m_osExpression(std::string(osExpression)), m_oVectors{}, m_oParser{},
m_adfResults{1}, m_bIsCompiled{false}
m_adfResults{1}, m_bIsCompiled{false}, m_bCompileFailed{false}
{
}

void Register(std::string_view osVariable, double *pdfValue)
{
m_oParser.DefineVar(std::string(osVariable), pdfValue);
try
{
m_oParser.DefineVar(std::string(osVariable), pdfValue);
}
catch (const mu::Parser::exception_type &)
{
CPLError(CE_Failure, CPLE_AppDefined, "Invalid variable name: %s",
std::string(osVariable).c_str());
m_bCompileFailed = true;
}
}

CPLErr Compile()
{
if (m_bCompileFailed)
{
return CE_Failure;
}

try
{
CPLString tmpExpression(m_osExpression);
Expand Down Expand Up @@ -99,6 +113,7 @@ class MuParserExpression::Impl
mu::Parser m_oParser;
std::vector<double> m_adfResults;
bool m_bIsCompiled;
bool m_bCompileFailed;
};

MuParserExpression::MuParserExpression(std::string_view osExpression)
Expand Down

0 comments on commit f4ae692

Please sign in to comment.