summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2014-03-07 15:40:53 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2014-03-07 16:03:10 +0100
commita07b169834a11f60a2baa98f9735c2dfe7e4a1ee (patch)
tree8814f4ea59e0fc4ecd3eabc9516b405b9d372003
parent7c1ee212b7fa1568a12a0a656b02cae01cb1cb6c (diff)
downloadlibgit2-a07b169834a11f60a2baa98f9735c2dfe7e4a1ee.tar.gz
branch: fix leak when checking against HEAD
We look up a reference in order to figure out if it's the current branch, which we need to free once we're done with the check. As a bonus, only perform the check when we're passed the force flag, as it's a useless check otherwise.
-rw-r--r--src/branch.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/branch.c b/src/branch.c
index 1ebaf8e24..7c888729d 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -66,16 +66,22 @@ int git_branch_create(
assert(branch_name && commit && ref_out);
assert(git_object_owner((const git_object *)commit) == repository);
- if (git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) {
- if ((is_head = git_branch_is_head(branch)) < 0) {
- error = is_head;
+
+ if (force && git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) {
+ error = git_branch_is_head(branch);
+ git_reference_free(branch);
+ branch = NULL;
+
+ if (error < 0)
goto cleanup;
- }
+
+ is_head = error;
}
if (is_head && force) {
giterr_set(GITERR_REFERENCE, "Cannot force update branch '%s' as it is "
- "the current HEAD of the repository.", git_reference_name(branch));
+ "the current HEAD of the repository.", branch_name);
+ error = -1;
goto cleanup;
}