Skip to content

Commit cd1de48

Browse files
fix: detect gaps in bindKeywordArguments and add test for round(base=1)
Agent-Logs-Url: https://github.com/Ultimaker/CuraFormulaeEngine/sessions/bb29b144-a878-4363-8aec-f3d1835aad79 Co-authored-by: casperlamboo <6638028+casperlamboo@users.noreply.github.com>
1 parent fc714b3 commit cd1de48

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/ast/fn_application_expr.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ std::optional<std::vector<eval::Value>> bindKeywordArguments(
6363
}
6464
bound.push_back(slot.value());
6565
}
66+
67+
// If any slot beyond the bound range has a value there is a gap (a required
68+
// earlier argument is missing while a later one was supplied via keyword).
69+
// Return nullopt so the caller can report InvalidNumberOfArguments.
70+
for (size_t i = bound.size(); i < slots.size(); ++i)
71+
{
72+
if (slots[i].has_value())
73+
{
74+
return std::nullopt;
75+
}
76+
}
77+
6678
return bound;
6779
}
6880

tests/parser.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,21 @@ TEST_CASE("fn unknown keyword arg", "[parser, fn, keyword]")
21132113
REQUIRE(eval.error() == CuraFormulaeEngine::eval::Error::InvalidNumberOfArguments);
21142114
}
21152115

2116+
TEST_CASE("fn keyword arg with gap in positional args", "[parser, fn, keyword]")
2117+
{
2118+
// round(base=1) omits the required first argument 'x'; bindKeywordArguments
2119+
// should detect the gap and return InvalidNumberOfArguments instead of
2120+
// invoking RoundFunction with 0 args (which would cause out-of-bounds UB).
2121+
auto input = "round(base=1)"sv;
2122+
2123+
const auto message = CuraFormulaeEngine::parser::parse(input);
2124+
REQUIRE(message.has_value());
2125+
const auto &ast = message.value();
2126+
const auto eval = ast.evaluate(&CuraFormulaeEngine::env::std_env);
2127+
REQUIRE_FALSE(eval.has_value());
2128+
REQUIRE(eval.error() == CuraFormulaeEngine::eval::Error::InvalidNumberOfArguments);
2129+
}
2130+
21162131
TEST_CASE("fn any trailing comma", "[parser, fn]")
21172132
{
21182133
auto input = "any([True], )"sv;

0 commit comments

Comments
 (0)