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>
Patrick Steinhardt committed
Sep 30, 2024 at 11:13 UTC
a0f2a2f5813596053cf785302b2bbfa76cbd9783
2 files changed
+35
-6
builtin/pack-redundant.c
+34
-6
@@ -69,6 +69,15 @@ static inline void llist_init(struct llist **list)
69
(*list)->size = 0;
70
}
71
72
+static void llist_free(struct llist *list)
73
+{
74
+ for (struct llist_item *i = list->front, *next; i; i = next) {
75
+ next = i->next;
76
+ llist_item_put(i);
77
+ }
78
+ free(list);
79
+}
80
+
81
static struct llist * llist_copy(struct llist *list)
82
{
83
struct llist *ret;
@@ -206,6 +215,14 @@ static inline struct pack_list * pack_list_insert(struct pack_list **pl,
215
return p;
216
}
217
218
+static void pack_list_free(struct pack_list *pl)
219
+{
220
+ for (struct pack_list *next; pl; pl = next) {
221
+ next = pl->next;
222
+ free(pl);
223
+ }
224
+}
225
+
226
static inline size_t pack_list_size(struct pack_list *pl)
227
{
228
size_t ret = 0;
@@ -419,7 +436,8 @@ static void minimize(struct pack_list **min)
436
437
/* return if there are no objects missing from the unique set */
438
if (missing->size == 0) {
422
- free(missing);
439
+ llist_free(missing);
440
+ pack_list_free(non_unique);
441
return;
442
}
443
@@ -434,6 +452,8 @@ static void minimize(struct pack_list **min)
452
}
453
454
while (non_unique) {
455
+ struct pack_list *next;
456
+
457
/* sort the non_unique packs, greater size of remaining_objects first */
458
sort_pack_list(&non_unique);
459
if (non_unique->remaining_objects->size == 0)
@@ -444,8 +464,14 @@ static void minimize(struct pack_list **min)
464
for (pl = non_unique->next; pl && pl->remaining_objects->size > 0; pl = pl->next)
465
llist_sorted_difference_inplace(pl->remaining_objects, non_unique->remaining_objects);
466
447
- non_unique = non_unique->next;
467
+ next = non_unique->next;
468
+ free(non_unique);
469
+ non_unique = next;
470
}
471
+
472
+ pack_list_free(non_unique);
473
+ llist_free(unique_pack_objects);
474
+ llist_free(missing);
475
}
476
477
static void load_all_objects(void)
@@ -565,7 +591,6 @@ static void load_all(void)
591
int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) {
592
int i; int i_still_use_this = 0; struct pack_list *min = NULL, *red, *pl;
593
struct llist *ignore;
568
- struct object_id *oid;
594
char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
595
596
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
650
/* ignore objects given on stdin */
651
llist_init(&ignore);
652
if (!isatty(0)) {
653
+ struct object_id oid;
654
while (fgets(buf, sizeof(buf), stdin)) {
629
- oid = xmalloc(sizeof(*oid));
630
- if (get_oid_hex(buf, oid))
655
+ if (get_oid_hex(buf, &oid))
656
die("Bad object ID on stdin: %s", buf);
632
- llist_insert_sorted_unique(ignore, oid, NULL);
657
+ llist_insert_sorted_unique(ignore, &oid, NULL);
658
}
659
}
660
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
696
fprintf(stderr, "%luMB of redundant packs in total.\n",
697
(unsigned long)pack_set_bytecount(red)/(1024*1024));
698
699
+ pack_list_free(red);
700
+ pack_list_free(min);
701
+ llist_free(ignore);
702
return 0;
703
}
t/t5323-pack-redundant.sh
+1
@@ -34,6 +34,7 @@ relationship between packs and objects is as follows:
34
Px2 | s s s x x x
35
'
36
37
+TEST_PASSES_SANITIZE_LEAK=true
38
. ./test-lib.sh
39
40
main_repo=main.git