Skip to content

Fix memory leak in git_revparse()#7193

Open
bakersdozen123 wants to merge 1 commit intolibgit2:mainfrom
bakersdozen123:fix-revparse-leak
Open

Fix memory leak in git_revparse()#7193
bakersdozen123 wants to merge 1 commit intolibgit2:mainfrom
bakersdozen123:fix-revparse-leak

Conversation

@bakersdozen123
Copy link
Contributor

@bakersdozen123 bakersdozen123 commented Jan 12, 2026

If an error occurs when calling git_revparse_single() with revspec->to, revspec->from must be freed, since it was already successfully allocated.

Fixes #7184.

I didn't set revspec->from to NULL after freeing it, since I don't generally see that pattern throughout the repo.

It's possible revspec->from was not being freed on purpose, since the object isn't opaque, and after receiving an error code, the caller can check if it is non-NULL and free it. I leaned more toward making the function atomic, but I can see how the original behavior may be preferred, especially since the git_revspec docs explicitly mention that freeing from and to is up to the caller.

If the original behavior is preferred, I could instead add a note to the git_revparse docs about this. Something like:

If an error occurs parsing to, make sure to free from.

Just to highlight that an error does not necessarily mean nothing has been allocated.

If an error occurs when calling `git_revparse_single()` with
`revspec->to`, `revspec->from` must be freed, since it was already
successfully allocated.
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.

[Bug Report] Memory Leak in git_revparse on Partial Parse Failure

1 participant