]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/pack-redundant: fix various memory leaks
authorPatrick Steinhardt <ps@pks.im>
Mon, 30 Sep 2024 09:13:32 +0000 (11:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Sep 2024 18:23:04 +0000 (11:23 -0700)
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>
builtin/pack-redundant.c
t/t5323-pack-redundant.sh

index 81f4494d466adef4d6a38874cb6576c644ad9358..580961300283a40ff5de87deaa32bf1b0a3a56bc 100644 (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;
 }
index 8dbbcc5e51c06d7c5f56fcb3107860fcb66a5106..4e18f5490a8e4a0c5e9914a368e2aa10c67eef69 100755 (executable)
@@ -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