Skip to content

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

Merged
soutaro merged 1 commit into
ruby:masterfrom
jackorp:fix_resource_leak
Feb 2, 2024
Merged

soutaro merged 1 commit into
ruby:masterfrom
jackorp:fix_resource_leak

Conversation

@jackorp

@jackorp jackorp commented Feb 1, 2024

Copy link
Copy Markdown
Contributor

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|   

@soutaro soutaro left a comment

Copy link
Copy Markdown
Member

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants