notes: fix memory leaks
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
diff --git a/src/notes.c b/src/notes.c
index efbdbab..0dfd3f8 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -56,7 +56,7 @@ static int find_subtree_r(git_tree **out, git_tree *root,
error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
if (error == GIT_EEXISTS) {
- return git_tree_lookup(out, repo, git_object_id((const git_object *)root));
+ return git_tree_lookup(out, repo, git_tree_id(root));
}
if (error < 0)
@@ -64,13 +64,7 @@ static int find_subtree_r(git_tree **out, git_tree *root,
*fanout += 2;
error = find_subtree_r(out, subtree, repo, target, fanout);
-
- /*
- * root is not ours to free, and the last subtree is the
- * one being returned => we only need to free the subtrees in-between
- */
- if (*out != subtree)
- git_tree_free(subtree);
+ git_tree_free(subtree);
return error;
}
@@ -153,7 +147,7 @@ static int manipulate_note_in_tree_r(
int current_error))
{
int error = -1;
- git_tree *subtree = NULL;
+ git_tree *subtree = NULL, *new = NULL;
char subtree_name[3];
error = find_subtree_in_current_level(
@@ -176,7 +170,7 @@ static int manipulate_note_in_tree_r(
/* An existing fanout has been found, let's dig deeper */
error = manipulate_note_in_tree_r(
- out, repo, subtree, note_oid, annotated_object_sha,
+ &new, repo, subtree, note_oid, annotated_object_sha,
fanout + 2, note_exists_cb, note_notfound_cb);
if (error < 0)
@@ -185,10 +179,12 @@ static int manipulate_note_in_tree_r(
strncpy(subtree_name, annotated_object_sha + fanout, 2);
subtree_name[2] = '\0';
- error = tree_write(out, repo, parent,
- git_object_id((const git_object *)(*out)), subtree_name, 0040000);
+ error = tree_write(out, repo, parent, git_tree_id(new),
+ subtree_name, 0040000);
+
cleanup:
+ git_tree_free(new);
git_tree_free(subtree);
return error;
}
diff --git a/tests-clar/notes/notes.c b/tests-clar/notes/notes.c
index 5f7f5a9..e138778 100644
--- a/tests-clar/notes/notes.c
+++ b/tests-clar/notes/notes.c
@@ -23,6 +23,8 @@ static void assert_note_equal(git_note *note, char *message, git_oid *note_oid)
cl_git_pass(git_blob_lookup(&blob, _repo, note_oid));
cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob));
+
+ git_blob_free(blob);
}
static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message)