worktrees: cleanup some memory leaks Be sure to clean up looked up references. Free buffers instead of merely clearing them. Use `git__free` instead of `free`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/src/branch.c b/src/branch.c
index 7a83b83..fe4955a 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -130,14 +130,16 @@ int git_branch_create_from_annotated(
static int branch_equals(git_repository *repo, const char *path, void *payload)
{
git_reference *branch = (git_reference *) payload;
- git_reference *head;
- int equal;
+ git_reference *head = NULL;
+ int equal = 0;
if (git_reference__read_head(&head, repo, path) < 0 ||
- git_reference_type(head) != GIT_REF_SYMBOLIC)
- return 0;
+ git_reference_type(head) != GIT_REF_SYMBOLIC)
+ goto done;
equal = !git__strcmp(head->target.symbolic, branch->name);
+
+done:
git_reference_free(head);
return equal;
}
diff --git a/src/refs.c b/src/refs.c
index 31410b7..632a529 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -277,8 +277,8 @@ int git_reference__read_head(
}
out:
- free(name);
- git_buf_clear(&reference);
+ git__free(name);
+ git_buf_free(&reference);
return error;
}
diff --git a/src/repository.c b/src/repository.c
index 707b7b7..f8d19eb 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -2132,7 +2132,8 @@ int git_repository_head_for_worktree(git_reference **out, git_repository *repo,
out:
if (error)
git_reference_free(head);
- git_buf_clear(&path);
+
+ git_buf_free(&path);
return error;
}