Skip to content

Commit d4cd2dd

Browse files
committed
Merge branch 'ds/commit-graph-leakfix'
Code clean-up. * ds/commit-graph-leakfix: commit-graph: reduce initial oid allocation builtin/commit-graph.c: UNLEAK variables commit-graph: clean up leaked memory during write
2 parents fa54ccc + 53c3667 commit d4cd2dd

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Diff for: builtin/commit-graph.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static int graph_verify(int argc, const char **argv)
6464
if (!graph)
6565
return 0;
6666

67+
UNLEAK(graph);
6768
return verify_commit_graph(the_repository, graph);
6869
}
6970

@@ -89,10 +90,8 @@ static int graph_read(int argc, const char **argv)
8990
graph_name = get_commit_graph_filename(opts.obj_dir);
9091
graph = load_commit_graph_one(graph_name);
9192

92-
if (!graph) {
93-
UNLEAK(graph_name);
93+
if (!graph)
9494
die("graph file %s does not exist", graph_name);
95-
}
9695

9796
FREE_AND_NULL(graph_name);
9897

@@ -115,7 +114,7 @@ static int graph_read(int argc, const char **argv)
115114
printf(" large_edges");
116115
printf("\n");
117116

118-
free_commit_graph(graph);
117+
UNLEAK(graph);
119118

120119
return 0;
121120
}
@@ -170,6 +169,8 @@ static int graph_write(int argc, const char **argv)
170169
pack_indexes = &lines;
171170
if (opts.stdin_commits)
172171
commit_hex = &lines;
172+
173+
UNLEAK(buf);
173174
}
174175

175176
write_commit_graph(opts.obj_dir,
@@ -178,7 +179,7 @@ static int graph_write(int argc, const char **argv)
178179
opts.append,
179180
1);
180181

181-
string_list_clear(&lines, 0);
182+
UNLEAK(lines);
182183
return 0;
183184
}
184185

Diff for: commit-graph.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,12 @@ static int add_ref_to_list(const char *refname,
739739
void write_commit_graph_reachable(const char *obj_dir, int append,
740740
int report_progress)
741741
{
742-
struct string_list list;
742+
struct string_list list = STRING_LIST_INIT_DUP;
743743

744-
string_list_init(&list, 1);
745744
for_each_ref(add_ref_to_list, &list);
746745
write_commit_graph(obj_dir, NULL, &list, append, report_progress);
746+
747+
string_list_clear(&list, 0);
747748
}
748749

749750
void write_commit_graph(const char *obj_dir,
@@ -768,7 +769,7 @@ void write_commit_graph(const char *obj_dir,
768769
return;
769770

770771
oids.nr = 0;
771-
oids.alloc = approximate_object_count() / 4;
772+
oids.alloc = approximate_object_count() / 32;
772773
oids.progress = NULL;
773774
oids.progress_done = 0;
774775

@@ -813,6 +814,7 @@ void write_commit_graph(const char *obj_dir,
813814
die(_("error opening index for %s"), packname.buf);
814815
for_each_object_in_pack(p, add_packed_commits, &oids, 0);
815816
close_pack(p);
817+
free(p);
816818
}
817819
stop_progress(&oids.progress);
818820
strbuf_release(&packname);
@@ -895,9 +897,11 @@ void write_commit_graph(const char *obj_dir,
895897
compute_generation_numbers(&commits, report_progress);
896898

897899
graph_name = get_commit_graph_filename(obj_dir);
898-
if (safe_create_leading_directories(graph_name))
900+
if (safe_create_leading_directories(graph_name)) {
901+
UNLEAK(graph_name);
899902
die_errno(_("unable to create leading directories of %s"),
900903
graph_name);
904+
}
901905

902906
hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
903907
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
@@ -942,9 +946,9 @@ void write_commit_graph(const char *obj_dir,
942946
finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
943947
commit_lock_file(&lk);
944948

949+
free(graph_name);
950+
free(commits.list);
945951
free(oids.list);
946-
oids.alloc = 0;
947-
oids.nr = 0;
948952
}
949953

950954
#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2

0 commit comments

Comments
 (0)