Git fork

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>

authored by

Damien Robert and committed by
Junio C Hamano
796d61cd 274b9cc2

+24 -4
+15
midx.c
··· 923 923 cur_chunk = 0; 924 924 num_chunks = large_offsets_needed ? 5 : 4; 925 925 926 + if (packs.nr - dropped_packs == 0) { 927 + error(_("no pack files to index.")); 928 + result = 1; 929 + goto cleanup; 930 + } 931 + 926 932 written = write_midx_header(f, num_chunks, packs.nr - dropped_packs); 927 933 928 934 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES; ··· 1122 1128 if (oid_fanout1 > oid_fanout2) 1123 1129 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"), 1124 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; 1125 1140 } 1126 1141 1127 1142 if (flags & MIDX_PROGRESS)
+9 -4
t/t5319-multi-pack-index.sh
··· 42 42 EOF 43 43 ' 44 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 49 54 ' 50 55 51 56 generate_objects () {
t/t5319/no-objects.midx

This is a binary file and will not be displayed.