diff --git a/include/inja/node.hpp b/include/inja/node.hpp index 1946805..6eaac54 100644 --- a/include/inja/node.hpp +++ b/include/inja/node.hpp @@ -143,7 +143,7 @@ public: Op operation; std::string name; - int number_args; // Should also be negative -> -1 for unknown number + int number_args; // Can also be negative -> -1 for unknown number std::vector> arguments; CallbackFunction callback; diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index 3fbbfcc..c490a22 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -76,6 +76,10 @@ class Parser { auto function = operator_stack.top(); operator_stack.pop(); + if (static_cast(arguments.size()) < function->number_args) { + throw_parser_error("too few arguments"); + } + for (int i = 0; i < function->number_args; ++i) { function->arguments.insert(function->arguments.begin(), arguments.back()); arguments.pop_back(); diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index 2feee8b..133ac8a 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -483,7 +483,7 @@ public: Op operation; std::string name; - int number_args; // Should also be negative -> -1 for unknown number + int number_args; // Can also be negative -> -1 for unknown number std::vector> arguments; CallbackFunction callback; @@ -1490,6 +1490,10 @@ class Parser { auto function = operator_stack.top(); operator_stack.pop(); + if (static_cast(arguments.size()) < function->number_args) { + throw_parser_error("too few arguments"); + } + for (int i = 0; i < function->number_args; ++i) { function->arguments.insert(function->arguments.begin(), arguments.back()); arguments.pop_back(); diff --git a/test/test-functions.cpp b/test/test-functions.cpp index 40a9795..432d7a6 100644 --- a/test/test-functions.cpp +++ b/test/test-functions.cpp @@ -35,7 +35,8 @@ TEST_CASE("functions") { CHECK(env.render("{{ 5^3 }}", data) == "125"); CHECK(env.render("{{ 5 + 12 + 4 * (4 - (1 + 1))^2 - 75 * 1 }}", data) == "-42"); - // CHECK_THROWS_WITH(env.render("{{ +1 }}", data), "[inja.exception.render_error] (at 1:4) empty expression"); + CHECK_THROWS_WITH(env.render("{{ +1 }}", data), "[inja.exception.parser_error] (at 1:7) too few arguments"); + CHECK_THROWS_WITH(env.render("{{ 1 + }}", data), "[inja.exception.parser_error] (at 1:8) too few arguments"); } SUBCASE("upper") {