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

Conversation

jbohanon
Copy link

@jbohanon jbohanon commented Mar 15, 2024

The variable result was a dangling reference when returning from Parser::parse function due to internals of parse_into, so we create a std::unique_ptr of the template and return that instead.

This is to resolve the static analysis warning generated in our envoy repos:

analyze-build: INFO: In file included from source/extensions/filters/http/transformation/inja_transformer.cc:1:
analyze-build: INFO: In file included from ./source/extensions/filters/http/transformation/inja_transformer.h:17:
analyze-build: INFO: bazel-out/k8-fastbuild/bin/external/inja/_virtual_includes/inja-lib/inja/inja.hpp:2065:5: warning: Address of stack memory associated with local variable 'result' is still referred to by the stack variable 'parser' upon returning to the caller.  This will be a dangling reference [core.StackAddressEscape]
analyze-build: INFO:     return result;
analyze-build: INFO:     ^~~~~~~~~~~~~

@jbohanon
Copy link
Author

jbohanon commented Mar 18, 2024

Currently the Parser::parse function is called from the same scope as the Parser is itself created. With this change, if the parser were to ever outlive the returned unique_ptr from the parse function, we would run into issues again. This is because fundamentally the issue is that the underlying memory from the Template object returned from parse is accessed and depended upon by the Parser's lexer. We could solve this by stashing the unique_ptrs in a vector on the Parser, but that feels hacky

tl;dr: the returned std::unique_ptr<Template> must outlive the Parser that created it.

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

@@ -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.

@jbohanon jbohanon changed the title fix dangling reference in parser::Parse with shared_ptr fix dangling reference in parser::Parse with unique_ptr Mar 18, 2024
@jbohanon jbohanon merged commit 1ee6ec1 into envoy-gloo-main Mar 18, 2024
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants