Skip to content

Commit a0f2a2f

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64fe1e4 commit a0f2a2f

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

builtin/pack-redundant.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ static inline void llist_init(struct llist **list)
6969
(*list)->size = 0;
7070
}
7171

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+
7281
static struct llist * llist_copy(struct llist *list)
7382
{
7483
struct llist *ret;
@@ -206,6 +215,14 @@ static inline struct pack_list * pack_list_insert(struct pack_list **pl,
206215
return p;
207216
}
208217

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+
209226
static inline size_t pack_list_size(struct pack_list *pl)
210227
{
211228
size_t ret = 0;
@@ -419,7 +436,8 @@ static void minimize(struct pack_list **min)
419436

420437
/* return if there are no objects missing from the unique set */
421438
if (missing->size == 0) {
422-
free(missing);
439+
llist_free(missing);
440+
pack_list_free(non_unique);
423441
return;
424442
}
425443

@@ -434,6 +452,8 @@ static void minimize(struct pack_list **min)
434452
}
435453

436454
while (non_unique) {
455+
struct pack_list *next;
456+
437457
/* sort the non_unique packs, greater size of remaining_objects first */
438458
sort_pack_list(&non_unique);
439459
if (non_unique->remaining_objects->size == 0)
@@ -444,8 +464,14 @@ static void minimize(struct pack_list **min)
444464
for (pl = non_unique->next; pl && pl->remaining_objects->size > 0; pl = pl->next)
445465
llist_sorted_difference_inplace(pl->remaining_objects, non_unique->remaining_objects);
446466

447-
non_unique = non_unique->next;
467+
next = non_unique->next;
468+
free(non_unique);
469+
non_unique = next;
448470
}
471+
472+
pack_list_free(non_unique);
473+
llist_free(unique_pack_objects);
474+
llist_free(missing);
449475
}
450476

451477
static void load_all_objects(void)
@@ -565,7 +591,6 @@ static void load_all(void)
565591
int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) {
566592
int i; int i_still_use_this = 0; struct pack_list *min = NULL, *red, *pl;
567593
struct llist *ignore;
568-
struct object_id *oid;
569594
char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
570595

571596
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
625650
/* ignore objects given on stdin */
626651
llist_init(&ignore);
627652
if (!isatty(0)) {
653+
struct object_id oid;
628654
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))
631656
die("Bad object ID on stdin: %s", buf);
632-
llist_insert_sorted_unique(ignore, oid, NULL);
657+
llist_insert_sorted_unique(ignore, &oid, NULL);
633658
}
634659
}
635660
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
671696
fprintf(stderr, "%luMB of redundant packs in total.\n",
672697
(unsigned long)pack_set_bytecount(red)/(1024*1024));
673698

699+
pack_list_free(red);
700+
pack_list_free(min);
701+
llist_free(ignore);
674702
return 0;
675703
}

t/t5323-pack-redundant.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ relationship between packs and objects is as follows:
3434
Px2 | s s s x x x
3535
'
3636

37+
TEST_PASSES_SANITIZE_LEAK=true
3738
. ./test-lib.sh
3839

3940
main_repo=main.git

0 commit comments

Comments
 (0)