Thanks to visit codestin.com
Credit goes to github.com

Skip to content

blame: increment reference count for origin's commit #3842

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

Merged
merged 1 commit into from
Jun 29, 2016

Conversation

pks-t
Copy link
Member

@pks-t pks-t commented Jun 27, 2016

blame: do not decrement commit refcount in make_origin

When we create a blame origin, we try to look up the blob that is
to be blamed at a certain revision. When this lookup fails, e.g.
because the file did not exist at that certain revision, we fail
to create the blame origin and return NULL. The blame origin
that we have just allocated is thereby free'd with
origin_decref.

The origin_decref function does not only decrement reference
counts for the blame origin, though, but also for its commit and
blob. When this is done in the error case, we will cause an
uneven reference count for these objects. This may result in
hard-to-debug failures at seemingly unrelated code paths, where
we try to access these objects when they in fact have already
been free'd.

Fix the issue by refactoring make_origin such that we only
allocate the object after the only function that may fail so that
we do not have to call origin_decref at all. Also fix the
pass_blame function, which indirectly calls make_origin, to
free the commit when make_origin failed.

@pks-t
Copy link
Member Author

pks-t commented Jun 27, 2016

Ok, I revert my previous comment about no additional memory leaks. Seems like Valgrind just wasn't able to catch them. More fiddeling with refcounts then :/

@pks-t pks-t force-pushed the pks/double-free branch from 5e7f57f to 2176670 Compare June 29, 2016 12:42
When we create a blame origin, we try to look up the blob that is
to be blamed at a certain revision. When this lookup fails, e.g.
because the file did not exist at that certain revision, we fail
to create the blame origin and return `NULL`. The blame origin
that we have just allocated is thereby free'd with
`origin_decref`.

The `origin_decref` function does not only decrement reference
counts for the blame origin, though, but also for its commit and
blob. When this is done in the error case, we will cause an
uneven reference count for these objects. This may result in
hard-to-debug failures at seemingly unrelated code paths, where
we try to access these objects when they in fact have already
been free'd.

Fix the issue by refactoring `make_origin` such that we only
allocate the object after the only function that may fail so that
we do not have to call `origin_decref` at all. Also fix the
`pass_blame` function, which indirectly calls `make_origin`, to
free the commit when `make_origin` failed.
@pks-t
Copy link
Member Author

pks-t commented Jun 29, 2016

Phew. I've finally been able to get to a version that has no memory leaks in our test suite while seemingly fixing the issues encountered in #3793. Running the modified example of @ameily does not cause a segfault after a few seconds anymore. I've been running the test on the libgit2 repo now for 20 minutes and until now everything looks good.

These small issues are always a tad depressing when one looks at how long it took to fix related to the actual outcome, which most often looks rather trivial :/ But meh, whatever.

@ethomson
Copy link
Member

These small issues are always a tad depressing when one looks at how long it took to fix related to the actual outcome, which most often looks rather trivial :/ But meh, whatever.

Yeah, and blame is a special kind of terrible. :(

As always, thanks.

@ethomson ethomson merged commit d88e500 into libgit2:master Jun 29, 2016
@pks-t pks-t deleted the pks/double-free branch June 29, 2016 15:10
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.

2 participants