Skip to content

Commit

Permalink
tokenize: disallow zero-length matches
Browse files Browse the repository at this point in the history
  • Loading branch information
cebtenzzre committed Feb 4, 2025
1 parent 3f30c0f commit e97bb24
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/minja/minja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// SPDX-License-Identifier: MIT
#pragma once

#include <cassert>
#include <iostream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -2336,6 +2337,10 @@ class Parser {
throw std::runtime_error("Unexpected block: " + keyword);
}
} else if (std::regex_search(it, end, match, non_text_open_regex)) {
if (!match.position()) {
assert(match[0] == "{#");
throw std::runtime_error("Missing end of comment tag");
}
auto text_end = it + match.position();
text = std::string(it, text_end);
it = text_end;
Expand Down
1 change: 1 addition & 0 deletions tests/test-syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ TEST(SyntaxTest, SimpleCases) {
EXPECT_THAT([]() { render("{% if 1 %}{% else %}", {}, {}); }, ThrowsWithSubstr("Unterminated if"));
EXPECT_THAT([]() { render("{% if 1 %}{% else %}{% elif 1 %}{% endif %}", {}, {}); }, ThrowsWithSubstr("Unterminated if"));
EXPECT_THAT([]() { render("{% filter trim %}", {}, {}); }, ThrowsWithSubstr("Unterminated filter"));
EXPECT_THAT([]() { render("{# ", {}, {}); }, ThrowsWithSubstr("Missing end of comment tag"));
}

EXPECT_EQ(
Expand Down

0 comments on commit e97bb24

Please sign in to comment.