@matheusbsbs / matheus-bagliano / commits / 796d61cdc0
Fork 0

midx.c: fix an integer underflow

When verifying a midx index with 0 objects, the m->num_objects - 1 underflows and wraps around to 4294967295. Fix this both by checking that the midx contains at least one oid, and also that we don't write any midx when there is no packfiles. Update the tests to check that `git multi-pack-index write` does not write an midx when there is no objects, and another to check that `git multi-pack-index verify` warns when it verifies an midx with no objects. For this last test, use t5319/no-objects.midx which was generated by an older version of git. Signed-off-by: Damien Robert <damien.olivier.robert+git@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Damien Robert committed Mar 28, 2020 at 23:18 UTC 796d61cdc09c8f3068403965531f27fd11c739e9
3 files changed +24 -4
midx.c
+15
@@ -923,6 +923,12 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
923 cur_chunk = 0;
924 num_chunks = large_offsets_needed ? 5 : 4;
925
926 + if (packs.nr - dropped_packs == 0) {
927 + error(_("no pack files to index."));
928 + result = 1;
929 + goto cleanup;
930 + }
931 +
932 written = write_midx_header(f, num_chunks, packs.nr - dropped_packs);
933
934 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
@@ -1124,6 +1130,15 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
1130 i, oid_fanout1, oid_fanout2, i + 1);
1131 }
1132
1133 + if (m->num_objects == 0) {
1134 + midx_report(_("the midx contains no oid"));
1135 + /*
1136 + * Remaining tests assume that we have objects, so we can
1137 + * return here.
1138 + */
1139 + return verify_midx_error;
1140 + }
1141 +
1142 if (flags & MIDX_PROGRESS)
1143 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1144 m->num_objects - 1);
t/t5319-multi-pack-index.sh
+9 -4
@@ -42,10 +42,15 @@ test_expect_success 'setup' '
42 EOF
43 '
44
45 -test_expect_success 'write midx with no packs' '
46 - test_when_finished rm -f pack/multi-pack-index &&
47 - git multi-pack-index --object-dir=. write &&
48 - midx_read_expect 0 0 4 .
45 +test_expect_success "don't write midx with no packs" '
46 + test_must_fail git multi-pack-index --object-dir=. write &&
47 + test_path_is_missing pack/multi-pack-index
48 +'
49 +
50 +test_expect_success "Warn if a midx contains no oid" '
51 + cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
52 + test_must_fail git multi-pack-index verify &&
53 + rm $objdir/pack/multi-pack-index
54 '
55
56 generate_objects () {
t/t5319/no-objects.midx
Binary files /dev/null and b/t/t5319/no-objects.midx differ