fix more leaks in commit graph's add_node()
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 3f3c93d..3942b1d 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -257,6 +257,22 @@ advance_open_branches(struct got_commit_graph *graph,
return NULL;
}
+static void
+free_node(struct got_commit_graph_node *node)
+{
+ while (!SIMPLEQ_EMPTY(&node->child_ids)) {
+ struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
+ SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
+ free(child);
+ }
+ while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
+ struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
+ SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
+ free(pid);
+ }
+ free(node);
+}
+
static const struct got_error *
add_node(struct got_commit_graph_node **new_node,
struct got_commit_graph *graph, struct got_object_id *commit_id,
@@ -278,14 +294,14 @@ add_node(struct got_commit_graph_node **new_node,
SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
err = add_vertex(&node->parent_ids, qid->id);
if (err) {
- free(node);
+ free_node(node);
return err;
}
node->nparents++;
}
node->commit_timestamp = mktime(&commit->tm_committer);
if (node->commit_timestamp == -1) {
- free(node);
+ free_node(node);
return got_error_from_errno();
}
@@ -297,10 +313,10 @@ add_node(struct got_commit_graph_node **new_node,
*new_node = node;
} else if (err->code == GOT_ERR_OBJ_EXISTS) {
err = NULL;
- free(node);
+ free_node(node);
node = existing_node;
} else {
- free(node);
+ free_node(node);
return err;
}
@@ -328,7 +344,7 @@ done:
if (err) {
free_node:
if (node != existing_node)
- free(node);
+ free_node(node);
*new_node = NULL;
}
return err;
@@ -491,27 +507,17 @@ got_commit_graph_fetch_commits_up_to(int *nfetched,
}
static void
-free_graph_node(struct got_object_id *id, void *data, void *arg)
+free_node_iter(struct got_object_id *id, void *data, void *arg)
{
struct got_commit_graph_node *node = data;
- while (!SIMPLEQ_EMPTY(&node->child_ids)) {
- struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
- SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
- free(child);
- }
- while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
- struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
- SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
- free(pid);
- }
- free(node);
+ free_node(node);
}
void
got_commit_graph_close(struct got_commit_graph *graph)
{
got_object_idset_free(graph->open_branches);
- got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
+ got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
got_object_idset_free(graph->node_ids);
free(graph);
}