Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dangling reference in parser::Parse with unique_ptr #8

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Environment {

Template parse(std::string_view input) {
Parser parser(parser_config, lexer_config, template_storage, function_storage);
return parser.parse(input, input_path);
return *parser.parse(input, input_path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are doing parse just on config time, right? if so i'm ok with this copy.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we are only parsing at config time. I will add a comment to the area where this is called in envoy-gloo to explicitly call out that we should only be parsing at config time.

}

Template parse_template(const std::string& filename) {
Expand Down
6 changes: 3 additions & 3 deletions include/inja/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,9 @@ class Parser {
const FunctionStorage& function_storage)
: config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) {}

Template parse(std::string_view input, std::string_view path) {
auto result = Template(static_cast<std::string>(input));
parse_into(result, path);
std::shared_ptr<Template> parse(std::string_view input, std::string_view path) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use unique_ptr here? i don't see this shared anywhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great callout, thanks! Made that change

auto result = std::make_shared<Template>(static_cast<std::string>(input));
parse_into(*result, path);
return result;
}

Expand Down
8 changes: 4 additions & 4 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2059,9 +2059,9 @@ class Parser {
const FunctionStorage& function_storage)
: config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) {}

Template parse(std::string_view input, std::string_view path) {
auto result = Template(static_cast<std::string>(input));
parse_into(result, path);
std::shared_ptr<Template> parse(std::string_view input, std::string_view path) {
auto result = std::make_shared<Template>(static_cast<std::string>(input));
parse_into(*result, path);
return result;
}

Expand Down Expand Up @@ -2834,7 +2834,7 @@ class Environment {

Template parse(std::string_view input) {
Parser parser(parser_config, lexer_config, template_storage, function_storage);
return parser.parse(input, input_path);
return *parser.parse(input, input_path);
}

Template parse_template(const std::string& filename) {
Expand Down
Loading