Skip to content
Snippets Groups Projects
Commit 2e8d9af2 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Forbid implicit conversion of negative integer to N or (N) arguments

- add missing non-negativity check for tuples of N that are
initialized by a single value
- add related tests
parent d93dd7ad
No related branches found
No related tags found
1 merge request!145git subrepo clone git@gitlab.com:OlMon/org-themes.git packages/org-themes
......@@ -52,15 +52,26 @@ class BuiltinFunctionProcessor : public INodeProcessor
ExecutionPolicy context_exec_policy{exec_policy,
ExecutionPolicy::Context{-1, std::make_shared<ExecutionPolicy::Context::Values>(
m_argument_converters.size())}};
if (m_argument_converters.size() == 1) {
try {
m_argument_converters[0]->convert(context_exec_policy, m_argument_node.execute(context_exec_policy));
}
catch (std::domain_error& e) {
throw ParseError(e.what(), m_argument_node.begin());
}
} else {
AggregateDataVariant argument_values{
std::get<AggregateDataVariant>(m_argument_node.execute(context_exec_policy))};
for (size_t i = 0; i < m_argument_converters.size(); ++i) {
try {
m_argument_converters[i]->convert(context_exec_policy, std::move(argument_values[i]));
}
catch (std::domain_error& e) {
throw ParseError(e.what(), m_argument_node.children[i]->begin());
}
}
}
if (SignalManager::pauseOnError()) {
......
......@@ -222,6 +222,11 @@ class FunctionTupleArgumentConverter final : public IFunctionArgumentConverter
}
} else if constexpr (std::is_convertible_v<ValueT, ContentType> and not is_tiny_vector_v<ContentType> and
not is_tiny_matrix_v<ContentType>) {
if constexpr (std::is_same_v<ContentType, uint64_t> and std::is_same_v<ValueT, int64_t>) {
if (v < 0) {
throw std::domain_error("trying to convert negative value (" + std::to_string(v) + ")");
}
}
exec_policy.currentContext()[m_argument_id] = std::move(TupleType{static_cast<ContentType>(v)});
} else {
throw UnexpectedError(std::string{"cannot convert '"} + demangle<ValueT>() + "' to '" +
......
......@@ -368,5 +368,34 @@ runtimeError();
CHECK_AST_THROWS_WITH(data, error);
}
SECTION("negative Z to N conversion")
{
std::string_view data = R"(
NtoR(3);
NtoR(-4);
)";
CHECK_AST_THROWS_WITH(data, std::string{"trying to convert negative value (-4)"});
}
SECTION("negative Z in list to (N) conversion")
{
std::string_view data = R"(
tuple_NtoR(2);
tuple_NtoR(-1);
)";
CHECK_AST_THROWS_WITH(data, std::string{"trying to convert negative value (-1)"});
}
SECTION("negative Z in list to (N) conversion")
{
std::string_view data = R"(
tuple_NtoR((3, 2, -3, 2));
)";
CHECK_AST_THROWS_WITH(data, std::string{"trying to convert negative value (-3)"});
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment