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

Prevent resource leak with type/method-type parser when reaching EOF. #1742

Merged
merged 1 commit into from
Feb 2, 2024

Conversation

jackorp
Copy link
Contributor

@jackorp jackorp commented Feb 1, 2024

A static analyzer detected a resource leak. alloc_parser allocates via calloc. Then if the next_token type is EOF the parser is not freed and we return. I do not think this is registered with GC, since it is manually freed in the methods later on. Therefore my conclusion is that freeing it was forgotten, unless there is more going on than I am aware of.

Local run of bundle exec rake test passed with Ruby 3.3.0.

Output of the analyzer:

RESOURCE_LEAK:
ruby-3.3.0/.bundle/gems/rbs-3.4.0/ext/rbs_extension/parser.c:2755: alloc_fn: Storage is returned from allocation function "alloc_parser".
ruby-3.3.0/.bundle/gems/rbs-3.4.0/ext/rbs_extension/parser.c:2755: var_assign: Assigning: "parser" = storage returned from "alloc_parser(buffer, RB_FIX2INT(start_pos), RB_FIX2INT(end_pos), variables)".
ruby-3.3.0/.bundle/gems/rbs-3.4.0/ext/rbs_extension/parser.c:2758: leaked_storage: Variable "parser" going out of scope leaks the storage it points to.
# 2756|   
# 2757|     if (parser->next_token.type == pEOF) {
# 2758|->     return Qnil;
# 2759|     }
# 2760|   

RESOURCE_LEAK:
ruby-3.3.0/.bundle/gems/rbs-3.4.0/ext/rbs_extension/parser.c:2775: alloc_fn: Storage is returned from allocation function "alloc_parser".
ruby-3.3.0/.bundle/gems/rbs-3.4.0/ext/rbs_extension/parser.c:2775: var_assign: Assigning: "parser" = storage returned from "alloc_parser(buffer, RB_FIX2INT(start_pos), RB_FIX2INT(end_pos), variables)".
ruby-3.3.0/.bundle/gems/rbs-3.4.0/ext/rbs_extension/parser.c:2778: leaked_storage: Variable "parser" going out of scope leaks the storage it points to.
# 2776|   
# 2777|     if (parser->next_token.type == pEOF) {
# 2778|->     return Qnil;
# 2779|     }
# 2780|   

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

Thanks!

@soutaro soutaro added this pull request to the merge queue Feb 2, 2024
@soutaro soutaro added this to the RBS 3.5 milestone Feb 2, 2024
Merged via the queue into ruby:master with commit 6e99a43 Feb 2, 2024
15 checks passed
@soutaro soutaro added the Released PRs already included in the released version label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

2 participants