pack-objects: free packing_data in more places

The pack-objects internals use a packing_data struct to track what objects are part of the pack(s) being formed. Since these structures contain allocated fields, failing to appropriately free() them results in a leak. Plug that leak by introducing a clear_packing_data() function, and call it in the appropriate spots. This is a fairly straightforward leak to plug, since none of the callers expect to read any values or have any references to parts of the address space being freed. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Dec 14, 2023 at 17:23 UTC 66f0c71073ee5fe1c9d12d2952305a4793d7b43f
4 files changed +22
builtin/pack-objects.c
+1
@@ -4522,6 +4522,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
4522 reuse_packfile_objects);
4523
4524 cleanup:
4525 + clear_packing_data(&to_pack);
4526 list_objects_filter_release(&filter_options);
4527 strvec_clear(&rp);
4528
midx.c
+5
@@ -1603,8 +1603,13 @@ static int write_midx_internal(const char *object_dir,
1603 flags) < 0) {
1604 error(_("could not write multi-pack bitmap"));
1605 result = 1;
1606 + clear_packing_data(&pdata);
1607 + free(commits);
1608 goto cleanup;
1609 }
1610 +
1611 + clear_packing_data(&pdata);
1612 + free(commits);
1613 }
1614 /*
1615 * NOTE: Do not use ctx.entries beyond this point, since it might
pack-objects.c
+15
@@ -151,6 +151,21 @@ void prepare_packing_data(struct repository *r, struct packing_data *pdata)
151 init_recursive_mutex(&pdata->odb_lock);
152 }
153
154 +void clear_packing_data(struct packing_data *pdata)
155 +{
156 + if (!pdata)
157 + return;
158 +
159 + free(pdata->cruft_mtime);
160 + free(pdata->in_pack);
161 + free(pdata->in_pack_by_idx);
162 + free(pdata->in_pack_pos);
163 + free(pdata->index);
164 + free(pdata->layer);
165 + free(pdata->objects);
166 + free(pdata->tree_depth);
167 +}
168 +
169 struct object_entry *packlist_alloc(struct packing_data *pdata,
170 const struct object_id *oid)
171 {
pack-objects.h
+1
@@ -169,6 +169,7 @@ struct packing_data {
169 };
170
171 void prepare_packing_data(struct repository *r, struct packing_data *pdata);
172 +void clear_packing_data(struct packing_data *pdata);
173
174 /* Protect access to object database */
175 static inline void packing_data_lock(struct packing_data *pdata)