Skip to content

Commit f4dbdfc

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: clean up leaked memory during write
The write_commit_graph() method in commit-graph.c leaks some lits and strings during execution. In addition, a list of strings is leaked in write_commit_graph_reachable(). Clean these up so our memory checking is cleaner. Further, if we use a list of pack-files to find the commits, we can leak the packed_git structs after scanning them for commits. Running the following commands demonstrates the leak before and the fix after: * valgrind --leak-check=full ./git commit-graph write --reachable * valgrind --leak-check=full ./git commit-graph write --stdin-packs Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6b89a34 commit f4dbdfc

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

commit-graph.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,12 @@ static int add_ref_to_list(const char *refname,
693693
void write_commit_graph_reachable(const char *obj_dir, int append,
694694
int report_progress)
695695
{
696-
struct string_list list;
696+
struct string_list list = STRING_LIST_INIT_DUP;
697697

698-
string_list_init(&list, 1);
699698
for_each_ref(add_ref_to_list, &list);
700699
write_commit_graph(obj_dir, NULL, &list, append, report_progress);
700+
701+
string_list_clear(&list, 0);
701702
}
702703

703704
void write_commit_graph(const char *obj_dir,
@@ -764,6 +765,7 @@ void write_commit_graph(const char *obj_dir,
764765
die(_("error opening index for %s"), packname.buf);
765766
for_each_object_in_pack(p, add_packed_commits, &oids, 0);
766767
close_pack(p);
768+
free(p);
767769
}
768770
stop_progress(&oids.progress);
769771
strbuf_release(&packname);
@@ -846,9 +848,11 @@ void write_commit_graph(const char *obj_dir,
846848
compute_generation_numbers(&commits, report_progress);
847849

848850
graph_name = get_commit_graph_filename(obj_dir);
849-
if (safe_create_leading_directories(graph_name))
851+
if (safe_create_leading_directories(graph_name)) {
852+
UNLEAK(graph_name);
850853
die_errno(_("unable to create leading directories of %s"),
851854
graph_name);
855+
}
852856

853857
hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
854858
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
@@ -893,9 +897,9 @@ void write_commit_graph(const char *obj_dir,
893897
finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
894898
commit_lock_file(&lk);
895899

900+
free(graph_name);
901+
free(commits.list);
896902
free(oids.list);
897-
oids.alloc = 0;
898-
oids.nr = 0;
899903
}
900904

901905
#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2

0 commit comments

Comments
 (0)