Skip to content

Commit 796d61c

Browse files
Damien Robertgitster
authored andcommitted
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>
1 parent 274b9cc commit 796d61c

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

midx.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,12 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
923923
cur_chunk = 0;
924924
num_chunks = large_offsets_needed ? 5 : 4;
925925

926+
if (packs.nr - dropped_packs == 0) {
927+
error(_("no pack files to index."));
928+
result = 1;
929+
goto cleanup;
930+
}
931+
926932
written = write_midx_header(f, num_chunks, packs.nr - dropped_packs);
927933

928934
chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
@@ -1124,6 +1130,15 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
11241130
i, oid_fanout1, oid_fanout2, i + 1);
11251131
}
11261132

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+
11271142
if (flags & MIDX_PROGRESS)
11281143
progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
11291144
m->num_objects - 1);

t/t5319-multi-pack-index.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ test_expect_success 'setup' '
4242
EOF
4343
'
4444

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
4954
'
5055

5156
generate_objects () {

t/t5319/no-objects.midx

1.09 KB
Binary file not shown.

0 commit comments

Comments
 (0)