]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pack-bitmap: fix memory leak if load_bitmap() failed
authorTaylor Blau <me@ttaylorr.com>
Tue, 1 Jul 2025 05:32:07 +0000 (05:32 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Jul 2025 21:41:53 +0000 (14:41 -0700)
After going through the "failed" label, load_bitmap() will return -1,
and its caller (either prepare_bitmap_walk() or prepare_bitmap_git())
will then call free_bitmap_index().

That function would have done:

    struct stored_bitmap *sb;
    kh_foreach_value(b->bitmaps, sb {
      ewah_pool_free(sb->root);
      free(sb);
    });

, but won't since load_bitmap() already called kh_destroy_oid_map() and
NULL'd the "bitmaps" pointer from within its "failed" label. Thus if you
got part of the way through loading bitmap entries and then failed, you
would leak all of the previous entries that you were able to load
successfully.

The solution is to remove the error handling code in load_bitmap(), because
its caller will always call free_bitmap_index() in case of an error.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pack-bitmap.c

index ac6d62b980c5a8d086bb1b71f46df721d11b3ad4..fd19c22551638d3d524d32c69b9531c782252b89 100644 (file)
@@ -630,41 +630,28 @@ static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git,
        bitmap_git->ext_index.positions = kh_init_oid_pos();
 
        if (load_reverse_index(r, bitmap_git))
-               goto failed;
+               return -1;
 
        if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
                !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
                !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
                !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
-               goto failed;
+               return -1;
 
        if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
-               goto failed;
+               return -1;
 
        if (bitmap_git->base) {
                if (!bitmap_is_midx(bitmap_git))
                        BUG("non-MIDX bitmap has non-NULL base bitmap index");
                if (load_bitmap(r, bitmap_git->base, 1) < 0)
-                       goto failed;
+                       return -1;
        }
 
        if (!recursing)
                load_all_type_bitmaps(bitmap_git);
 
        return 0;
-
-failed:
-       munmap(bitmap_git->map, bitmap_git->map_size);
-       bitmap_git->map = NULL;
-       bitmap_git->map_size = 0;
-
-       kh_destroy_oid_map(bitmap_git->bitmaps);
-       bitmap_git->bitmaps = NULL;
-
-       kh_destroy_oid_pos(bitmap_git->ext_index.positions);
-       bitmap_git->ext_index.positions = NULL;
-
-       return -1;
 }
 
 static int open_pack_bitmap(struct repository *r,