repack: free geometry struct

When the program is ending, we call clear_pack_geometry() to free any resources in the pack_geometry struct. But the struct itself is allocated on the heap, and leak-checkers will complain about the resulting small leak. This one was marked by Coverity as a "new" leak, though it has existed since 0fabafd0b9 (builtin/repack.c: add '--geometric' option, 2021-02-22). This might be because recent unrelated changes in the file confused it about what is new and what is not. But regardless, it is worth addressing. We can fix it easily by free-ing the struct. We'll convert our "clear" function to "free", since the allocation happens in the matching init() function (though since there is only one call to each, and the struct is local to this file, it's mostly academic). Another option would be to put the struct on the stack rather than the heap. However, this gets tricky, as we check the pointer against NULL in several places to decide whether we're in geometric mode. Signed-off-by: Jeff King <peff@peff.net> Acked-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 8, 2023 at 14:50 UTC cb888bb6991bb10bddedf9ddc9651ec25da6137d
1 file changed +3 -5
builtin/repack.c
+3 -5
@@ -492,15 +492,13 @@ static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
492 return NULL;
493 }
494
495 -static void clear_pack_geometry(struct pack_geometry *geometry)
495 +static void free_pack_geometry(struct pack_geometry *geometry)
496 {
497 if (!geometry)
498 return;
499
500 free(geometry->pack);
501 - geometry->pack_nr = 0;
502 - geometry->pack_alloc = 0;
503 - geometry->split = 0;
501 + free(geometry);
502 }
503
504 struct midx_snapshot_ref_data {
@@ -1228,7 +1226,7 @@ cleanup:
1226 string_list_clear(&names, 1);
1227 string_list_clear(&existing_nonkept_packs, 0);
1228 string_list_clear(&existing_kept_packs, 0);
1231 - clear_pack_geometry(geometry);
1229 + free_pack_geometry(geometry);
1230
1231 return ret;
1232 }