Commit 5c6ae009 by Kirill A. Shutemov

refs: fix memory leak on rename

Current implementation of git_reference_rename() removes 'ref' from
loose cache, but not frees it. In result 'ref' is not reachable any more
and we have got memory leak.

Let's re-add 'ref' with corrected name to loose cache instead of
'new_ref' and free 'new_ref' properly.

'rollback' path seems leak too. git_reference_rename() need to be rewritten
for proper resource management.

Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
parent 89fb9d04
......@@ -1404,7 +1404,14 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
free(ref->name);
ref->name = new_ref->name;
if ((error = git_hashtable_insert2(ref->owner->references.loose_cache, new_ref->name, new_ref, (void **)&old_ref)) < GIT_SUCCESS)
/*
* No need in new_ref anymore. We created it to fix the change on disk.
* TODO: Refactoring required.
*/
new_ref->name = NULL;
reference_free(new_ref);
if ((error = git_hashtable_insert2(ref->owner->references.loose_cache, ref->name, ref, (void **)&old_ref)) < GIT_SUCCESS)
goto rollback;
/*
......@@ -1417,7 +1424,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
head_target = git_reference_target(head);
if (head_target && !strcmp(head_target, old_name))
if ((error = git_reference_create_symbolic(&head, new_ref->owner, "HEAD", new_ref->name, 1)) < GIT_SUCCESS)
if ((error = git_reference_create_symbolic(&head, ref->owner, "HEAD", ref->name, 1)) < GIT_SUCCESS)
goto rollback;
cleanup:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment