Skip to content

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Jan 20, 2025

argv[0], ie: the full buffer allocated by get_cmd_line_args, was going to be freed only if can_use_shortcut() was called. Since that is not always the case, add a fallback cleanup method.

Spotted using -fsanitize=address:

==1050100==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 512 byte(s) in 1 object(s) allocated from:
    #0 0x70591a2fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x705915490406 in get_cmd_line_args /home/federico/go/pkg/mod/github.com/containers/podman/v5@v5.2.5/pkg/rootless/rootless_linux.c:313

Fix was tested by running once again (after applying the patch) with asan.

Does this PR introduce a user-facing change?

None

@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 20, 2025

Mmmh i am asked to add a new test, but well i am a bit unsure how to test this one :)

@FedeDP FedeDP force-pushed the fix/get_cmd_line_args_memleak branch from 661a6ce to 114dfb3 Compare January 20, 2025 09:08
Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

@giuseppe PTAL

The argv0 is never used anywhere AFAICT so can it not be dropped completely?

@baude baude added the No New Tests Allow PR to proceed without adding regression tests label Jan 20, 2025
@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 21, 2025

The argv0 is never used anywhere AFAICT so can it not be dropped completely?

You mean from can_use_shortcut? It would make sense IMHO.

@Luap99
Copy link
Member

Luap99 commented Jan 21, 2025

You mean from can_use_shortcut? It would make sense IMHO.

Yes I do not see why this is there at all but maybe there is something about c I am not understanding. Properly best to wait for @giuseppe to comment.

{
can_shortcut = can_use_shortcut(argv);
// It was freed by can_use_shortcut() call
Copy link
Member

Choose a reason for hiding this comment

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

can we drop the autocleanup from can_use_shortcut so we don't have to deal with the special case here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed the changes (i added the coauthor, hope it is fine)

@giuseppe
Copy link
Member

sorry, I added a comment but forgot to send the review. Fixed now

@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 28, 2025

sorry, I added a comment but forgot to send the review. Fixed now

no prob, it happens all the time 😆

@@ -496,6 +494,8 @@ static void __attribute__((constructor)) init()
fprintf(stderr, "cannot retrieve cmd line");
_exit (EXIT_FAILURE);
}
// Even if unused, this is needed to ensure we properly free the memory
argv0 = argv[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We always let init be in charge of freeing argv0 now.

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks!

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 30, 2025
@Luap99
Copy link
Member

Luap99 commented Jan 30, 2025

Can you please squash the commits? I don't think the previous commit need to be part of the history.

`argv[0]`, ie: the full buffer allocated by `get_cmd_line_args`,
was going to be freed only if `can_use_shortcut()` was called.
Instead, let `init()` always manage `argv0` lifecycle.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP FedeDP force-pushed the fix/get_cmd_line_args_memleak branch from b3b3406 to 51fd6e9 Compare January 30, 2025 11:12
@FedeDP
Copy link
Contributor Author

FedeDP commented Jan 30, 2025

Sure! Done

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jan 30, 2025
Copy link
Contributor

openshift-ci bot commented Jan 30, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP, giuseppe, Luap99

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit 4fdd00e into containers:main Jan 30, 2025
79 checks passed
@FedeDP FedeDP deleted the fix/get_cmd_line_args_memleak branch January 30, 2025 13:16
@stale-locking-app stale-locking-app bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label May 1, 2025
@stale-locking-app stale-locking-app bot locked as resolved and limited conversation to collaborators May 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. No New Tests Allow PR to proceed without adding regression tests release-note-none
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants