index-pack: fix memory leaks

Fix various memory leaks in "git index-pack", due to how tightly coupled this command is with the revision walking this doesn't make any new tests pass. But e.g. this now passes, and had several failures before, i.e. we still have failures in tests 3, 5 etc., which are being skipped here. ./t5300-pack-object.sh --run=1-2,4,6-27,30-42 It is a bit odd that we'll free "opts.anomaly", since the "opts" is a "struct pack_idx_option" declared in pack.h. In pack-write.c there's a reset_pack_idx_option(), but it only wipes the contents, but doesn't free() anything. Doing this here in cmd_index_pack() is correct because while the struct is declared in pack.h, this code in builtin/index-pack.c (in read_v2_anomalous_offsets()) is what allocates the "opts.anomaly", so we should also free it here. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 4, 2022 at 19:32 UTC f2bcc69e7ecbd80fe1de81a196c97173f33785b5
1 file changed +5
builtin/index-pack.c
+5
@@ -1109,6 +1109,7 @@ static void *threaded_second_pass(void *data)
1109 list_add(&child->list, &work_head);
1110 base_cache_used += child->size;
1111 prune_base_data(NULL);
1112 + free_base_data(child);
1113 } else {
1114 /*
1115 * This child does not have its own children. It may be
@@ -1131,6 +1132,7 @@ static void *threaded_second_pass(void *data)
1132
1133 p = next_p;
1134 }
1135 + FREE_AND_NULL(child);
1136 }
1137 work_unlock();
1138 }
@@ -1424,6 +1426,7 @@ static void fix_unresolved_deltas(struct hashfile *f)
1426 * object).
1427 */
1428 append_obj_to_pack(f, d->oid.hash, data, size, type);
1429 + free(data);
1430 threaded_second_pass(NULL);
1431
1432 display_progress(progress, nr_resolved_deltas);
@@ -1703,6 +1706,7 @@ static void show_pack_info(int stat_only)
1706 i + 1,
1707 chain_histogram[i]);
1708 }
1709 + free(chain_histogram);
1710 }
1711
1712 int cmd_index_pack(int argc, const char **argv, const char *prefix)
@@ -1932,6 +1936,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1936 if (do_fsck_object && fsck_finish(&fsck_options))
1937 die(_("fsck error in pack objects"));
1938
1939 + free(opts.anomaly);
1940 free(objects);
1941 strbuf_release(&index_name_buf);
1942 strbuf_release(&rev_index_name_buf);