Skip to content

open:fix memory leak when passing NULL to git_repository_open_ext#5224

Merged
ethomson merged 2 commits into
libgit2:masterfrom
lrm29:check_if_repository_memory_leak
Sep 11, 2019
Merged

open:fix memory leak when passing NULL to git_repository_open_ext#5224
ethomson merged 2 commits into
libgit2:masterfrom
lrm29:check_if_repository_memory_leak

Conversation

@lrm29
Copy link
Copy Markdown
Contributor

@lrm29 lrm29 commented Sep 10, 2019

There is an example on the website for checking if a directory looks like a repository:

https://libgit2.org/docs/guides/101-samples/#repositories_openable

This example leaks memory. This pull request includes a test point which leaks memory and is caught by valgrind, plus a fix in git_repository_open_ext to dispose of buffers on the early return.

Comment thread src/repository.c Outdated
&gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs);

if (error < 0 || !repo_ptr)
if (error < 0 || !repo_ptr) {
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.

Instead of adding duplicate frees here, I would encourage you to jump to the cleanup label so that we can consolidate cleanup in a single place. That's the general pattern we try to use, it makes it less likely that we would end up freeing in one return path but not the other if we end up adding new allocations (for instance).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, I've updated the review. I initialised 'repo' to NULL in order to handle the cleanup case where error < 0 and repo_ptr is NULL.

@ethomson
Copy link
Copy Markdown
Member

Nice catch. A minor request for a stylistic tweak, if you don't mind.

@ethomson
Copy link
Copy Markdown
Member

Looks great! Thanks for finding this and the helpful pull request. Love that you added a test for it! 😃

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.

5 participants