Skip to content

Commit 8777ec1

Browse files
committed
Merge branch 'dr/midx-avoid-int-underflow'
When fed a midx that records no objects, some codepaths tried to loop from 0 through (num_objects-1), which, due to integer arithmetic wrapping around, made it nonsense operation with out of bounds array accesses. The code has been corrected to reject such an midx file. * dr/midx-avoid-int-underflow: midx.c: fix an integer underflow
2 parents 7a8e630 + 796d61c commit 8777ec1

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)