builtin/pack-redundant: fix various memory leaks

There are various different memory leaks in git-pack-redundant(1),
mostly caused by not even trying to free allocated memory. Fix them.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2024-09-30 11:13:32 +02:00 committed by Junio C Hamano
parent 64fe1e4a8c
commit a0f2a2f581
2 changed files with 35 additions and 6 deletions

View file

@ -69,6 +69,15 @@ static inline void llist_init(struct llist **list)
(*list)->size = 0;
}
static void llist_free(struct llist *list)
{
for (struct llist_item *i = list->front, *next; i; i = next) {
next = i->next;
llist_item_put(i);
}
free(list);
}
static struct llist * llist_copy(struct llist *list)
{
struct llist *ret;
@ -206,6 +215,14 @@ static inline struct pack_list * pack_list_insert(struct pack_list **pl,
return p;
}
static void pack_list_free(struct pack_list *pl)
{
for (struct pack_list *next; pl; pl = next) {
next = pl->next;
free(pl);
}
}
static inline size_t pack_list_size(struct pack_list *pl)
{
size_t ret = 0;
@ -419,7 +436,8 @@ static void minimize(struct pack_list **min)
/* return if there are no objects missing from the unique set */
if (missing->size == 0) {
free(missing);
llist_free(missing);
pack_list_free(non_unique);
return;
}
@ -434,6 +452,8 @@ static void minimize(struct pack_list **min)
}
while (non_unique) {
struct pack_list *next;
/* sort the non_unique packs, greater size of remaining_objects first */
sort_pack_list(&non_unique);
if (non_unique->remaining_objects->size == 0)
@ -444,8 +464,14 @@ static void minimize(struct pack_list **min)
for (pl = non_unique->next; pl && pl->remaining_objects->size > 0; pl = pl->next)
llist_sorted_difference_inplace(pl->remaining_objects, non_unique->remaining_objects);
non_unique = non_unique->next;
next = non_unique->next;
free(non_unique);
non_unique = next;
}
pack_list_free(non_unique);
llist_free(unique_pack_objects);
llist_free(missing);
}
static void load_all_objects(void)
@ -565,7 +591,6 @@ static void load_all(void)
int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) {
int i; int i_still_use_this = 0; struct pack_list *min = NULL, *red, *pl;
struct llist *ignore;
struct object_id *oid;
char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
if (argc == 2 && !strcmp(argv[1], "-h"))
@ -625,11 +650,11 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
/* ignore objects given on stdin */
llist_init(&ignore);
if (!isatty(0)) {
struct object_id oid;
while (fgets(buf, sizeof(buf), stdin)) {
oid = xmalloc(sizeof(*oid));
if (get_oid_hex(buf, oid))
if (get_oid_hex(buf, &oid))
die("Bad object ID on stdin: %s", buf);
llist_insert_sorted_unique(ignore, oid, NULL);
llist_insert_sorted_unique(ignore, &oid, NULL);
}
}
llist_sorted_difference_inplace(all_objects, ignore);
@ -671,5 +696,8 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
fprintf(stderr, "%luMB of redundant packs in total.\n",
(unsigned long)pack_set_bytecount(red)/(1024*1024));
pack_list_free(red);
pack_list_free(min);
llist_free(ignore);
return 0;
}

View file

@ -34,6 +34,7 @@ relationship between packs and objects is as follows:
Px2 | s s s x x x
'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
main_repo=main.git