]> git.ipfire.org Git - thirdparty/git.git/blobdiff - packfile.c
fuzz: add fuzz testing for packfile indices.
[thirdparty/git.git] / packfile.c
index 12db1a9d7d6251016627e6bc4ac80e55206489c5..86074a76e965087ed826e3170b867e995fd9e9fe 100644 (file)
@@ -80,10 +80,8 @@ void pack_report(void)
 static int check_packed_git_idx(const char *path, struct packed_git *p)
 {
        void *idx_map;
-       struct pack_idx_header *hdr;
        size_t idx_size;
-       uint32_t version, nr, i, *index;
-       int fd = git_open(path);
+       int fd = git_open(path), ret;
        struct stat st;
        const unsigned int hashsz = the_hash_algo->rawsz;
 
@@ -101,16 +99,32 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
        idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
        close(fd);
 
-       hdr = idx_map;
+       ret = load_idx(path, hashsz, idx_map, idx_size, p);
+
+       if (ret)
+               munmap(idx_map, idx_size);
+
+       return ret;
+}
+
+int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
+            size_t idx_size, struct packed_git *p)
+{
+       struct pack_idx_header *hdr = idx_map;
+       uint32_t version, nr, i, *index;
+
+       if (idx_size < 4 * 256 + hashsz + hashsz)
+               return error("index file %s is too small", path);
+       if (idx_map == NULL)
+               return error("empty data");
+
        if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
                version = ntohl(hdr->idx_version);
-               if (version < 2 || version > 2) {
-                       munmap(idx_map, idx_size);
+               if (version < 2 || version > 2)
                        return error("index file %s is version %"PRIu32
                                     " and is not supported by this binary"
                                     " (try upgrading GIT to a newer version)",
                                     path, version);
-               }
        } else
                version = 1;
 
@@ -120,10 +134,8 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
                index += 2;  /* skip index header */
        for (i = 0; i < 256; i++) {
                uint32_t n = ntohl(index[i]);
-               if (n < nr) {
-                       munmap(idx_map, idx_size);
+               if (n < nr)
                        return error("non-monotonic index %s", path);
-               }
                nr = n;
        }
 
@@ -135,10 +147,8 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
                 *  - hash of the packfile
                 *  - file checksum
                 */
-               if (idx_size != 4*256 + nr * (hashsz + 4) + hashsz + hashsz) {
-                       munmap(idx_map, idx_size);
+               if (idx_size != 4 * 256 + nr * (hashsz + 4) + hashsz + hashsz)
                        return error("wrong index v1 file size in %s", path);
-               }
        } else if (version == 2) {
                /*
                 * Minimum size:
@@ -157,20 +167,16 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
                unsigned long max_size = min_size;
                if (nr)
                        max_size += (nr - 1)*8;
-               if (idx_size < min_size || idx_size > max_size) {
-                       munmap(idx_map, idx_size);
+               if (idx_size < min_size || idx_size > max_size)
                        return error("wrong index v2 file size in %s", path);
-               }
                if (idx_size != min_size &&
                    /*
                     * make sure we can deal with large pack offsets.
                     * 31-bit signed offset won't be enough, neither
                     * 32-bit unsigned one will be.
                     */
-                   (sizeof(off_t) <= 4)) {
-                       munmap(idx_map, idx_size);
+                   (sizeof(off_t) <= 4))
                        return error("pack too large for current definition of off_t in %s", path);
-               }
        }
 
        p->index_version = version;
@@ -550,7 +556,7 @@ static int open_packed_git_1(struct packed_git *p)
        if (read_result != hashsz)
                return error("packfile %s signature is unavailable", p->pack_name);
        idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2;
-       if (hashcmp(hash, idx_hash))
+       if (!hasheq(hash, idx_hash))
                return error("packfile %s does not match index", p->pack_name);
        return 0;
 }
@@ -820,9 +826,8 @@ static void prepare_pack(const char *full_name, size_t full_name_len,
        struct packed_git *p;
        size_t base_len = full_name_len;
 
-       if (strip_suffix_mem(full_name, &base_len, ".idx")) {
-               if (data->m && midx_contains_pack(data->m, file_name))
-                       return;
+       if (strip_suffix_mem(full_name, &base_len, ".idx") &&
+           !(data->m && midx_contains_pack(data->m, file_name))) {
                /* Don't reopen a pack we already have. */
                for (p = data->r->objects->packed_git; p; p = p->next) {
                        size_t len;
@@ -842,6 +847,8 @@ static void prepare_pack(const char *full_name, size_t full_name_len,
        if (!report_garbage)
                return;
 
+       if (!strcmp(file_name, "multi-pack-index"))
+               return;
        if (ends_with(file_name, ".idx") ||
            ends_with(file_name, ".pack") ||
            ends_with(file_name, ".bitmap") ||
@@ -963,14 +970,17 @@ static void prepare_packed_git(struct repository *r)
 
        if (r->objects->packed_git_initialized)
                return;
-       prepare_multi_pack_index_one(r, r->objects->objectdir);
+       prepare_multi_pack_index_one(r, r->objects->objectdir, 1);
        prepare_packed_git_one(r, r->objects->objectdir, 1);
        prepare_alt_odb(r);
        for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
-               prepare_multi_pack_index_one(r, alt->path);
+               prepare_multi_pack_index_one(r, alt->path, 0);
                prepare_packed_git_one(r, alt->path, 0);
        }
        rearrange_packed_git(r);
+
+       r->objects->all_packs = NULL;
+
        prepare_packed_git_mru(r);
        r->objects->packed_git_initialized = 1;
 }
@@ -994,6 +1004,30 @@ struct multi_pack_index *get_multi_pack_index(struct repository *r)
        return r->objects->multi_pack_index;
 }
 
+struct packed_git *get_all_packs(struct repository *r)
+{
+       prepare_packed_git(r);
+
+       if (!r->objects->all_packs) {
+               struct packed_git *p = r->objects->packed_git;
+               struct multi_pack_index *m;
+
+               for (m = r->objects->multi_pack_index; m; m = m->next) {
+                       uint32_t i;
+                       for (i = 0; i < m->num_packs; i++) {
+                               if (!prepare_midx_pack(m, i)) {
+                                       m->packs[i]->next = p;
+                                       p = m->packs[i];
+                               }
+                       }
+               }
+
+               r->objects->all_packs = p;
+       }
+
+       return r->objects->all_packs;
+}
+
 struct list_head *get_packed_git_mru(struct repository *r)
 {
        prepare_packed_git(r);
@@ -1094,7 +1128,7 @@ void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1)
 {
        unsigned i;
        for (i = 0; i < p->num_bad_objects; i++)
-               if (!hashcmp(sha1, p->bad_object_sha1 + GIT_SHA1_RAWSZ * i))
+               if (hasheq(sha1, p->bad_object_sha1 + GIT_SHA1_RAWSZ * i))
                        return;
        p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
                                      st_mult(GIT_MAX_RAWSZ,
@@ -1110,8 +1144,8 @@ const struct packed_git *has_packed_and_bad(const unsigned char *sha1)
 
        for (p = the_repository->objects->packed_git; p; p = p->next)
                for (i = 0; i < p->num_bad_objects; i++)
-                       if (!hashcmp(sha1,
-                                    p->bad_object_sha1 + the_hash_algo->rawsz * i))
+                       if (hasheq(sha1,
+                                  p->bad_object_sha1 + the_hash_algo->rawsz * i))
                                return p;
        return NULL;
 }
@@ -1909,8 +1943,8 @@ static int fill_pack_entry(const struct object_id *oid,
        if (p->num_bad_objects) {
                unsigned i;
                for (i = 0; i < p->num_bad_objects; i++)
-                       if (!hashcmp(oid->hash,
-                                    p->bad_object_sha1 + the_hash_algo->rawsz * i))
+                       if (hasheq(oid->hash,
+                                  p->bad_object_sha1 + the_hash_algo->rawsz * i))
                                return 0;
        }
 
@@ -2008,7 +2042,7 @@ int for_each_packed_object(each_packed_object_fn cb, void *data,
        int pack_errors = 0;
 
        prepare_packed_git(the_repository);
-       for (p = the_repository->objects->packed_git; p; p = p->next) {
+       for (p = get_all_packs(the_repository); p; p = p->next) {
                if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
                        continue;
                if ((flags & FOR_EACH_OBJECT_PROMISOR_ONLY) &&