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 <martin.agren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Oct 3, 2018 at 10:12 UTC f4dbdfc4d514d4ea558703f5a234652dc68d83b2
1 file changed +9 -5
commit-graph.c
+9 -5
@@ -693,11 +693,12 @@ static int add_ref_to_list(const char *refname,
693 void write_commit_graph_reachable(const char *obj_dir, int append,
694 int report_progress)
695 {
696 - struct string_list list;
696 + struct string_list list = STRING_LIST_INIT_DUP;
697
698 - string_list_init(&list, 1);
698 for_each_ref(add_ref_to_list, &list);
699 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
700 +
701 + string_list_clear(&list, 0);
702 }
703
704 void write_commit_graph(const char *obj_dir,
@@ -764,6 +765,7 @@ void write_commit_graph(const char *obj_dir,
765 die(_("error opening index for %s"), packname.buf);
766 for_each_object_in_pack(p, add_packed_commits, &oids, 0);
767 close_pack(p);
768 + free(p);
769 }
770 stop_progress(&oids.progress);
771 strbuf_release(&packname);
@@ -846,9 +848,11 @@ void write_commit_graph(const char *obj_dir,
848 compute_generation_numbers(&commits, report_progress);
849
850 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);
853 die_errno(_("unable to create leading directories of %s"),
854 graph_name);
855 + }
856
857 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
858 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
@@ -893,9 +897,9 @@ void write_commit_graph(const char *obj_dir,
897 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
898 commit_lock_file(&lk);
899
900 + free(graph_name);
901 + free(commits.list);
902 free(oids.list);
897 - oids.alloc = 0;
898 - oids.nr = 0;
903 }
904
905 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2