Skip to content

Commit cc6af73

Browse files
derrickstoleegitster
authored andcommitted
multi-pack-index: verify object offsets
The 'git multi-pack-index verify' command must verify the object offsets stored in the multi-pack-index are correct. There are two ways the offset chunk can be incorrect: the pack-int-id and the object offset. Replace the BUG() statement with a die() statement, now that we may hit a bad pack-int-id during a 'verify' command on a corrupt multi-pack-index, and it is covered by a test. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d8ac9ee commit cc6af73

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

midx.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id)
197197
struct strbuf pack_name = STRBUF_INIT;
198198

199199
if (pack_int_id >= m->num_packs)
200-
BUG("bad pack-int-id");
200+
die(_("bad pack-int-id: %u (%u total packs"),
201+
pack_int_id, m->num_packs);
201202

202203
if (m->packs[pack_int_id])
203204
return 0;
@@ -970,5 +971,31 @@ int verify_midx_file(const char *object_dir)
970971
i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
971972
}
972973

974+
for (i = 0; i < m->num_objects; i++) {
975+
struct object_id oid;
976+
struct pack_entry e;
977+
off_t m_offset, p_offset;
978+
979+
nth_midxed_object_oid(&oid, m, i);
980+
if (!fill_midx_entry(&oid, &e, m)) {
981+
midx_report(_("failed to load pack entry for oid[%d] = %s"),
982+
i, oid_to_hex(&oid));
983+
continue;
984+
}
985+
986+
if (open_pack_index(e.p)) {
987+
midx_report(_("failed to load pack-index for packfile %s"),
988+
e.p->pack_name);
989+
break;
990+
}
991+
992+
m_offset = e.offset;
993+
p_offset = find_pack_entry_one(oid.hash, e.p);
994+
995+
if (m_offset != p_offset)
996+
midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
997+
i, oid_to_hex(&oid), m_offset, p_offset);
998+
}
999+
9731000
return verify_midx_error;
9741001
}

t/t5319-multi-pack-index.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ test_expect_success 'verify bad signature' '
176176
'
177177

178178
HASH_LEN=20
179+
NUM_OBJECTS=74
179180
MIDX_BYTE_VERSION=4
180181
MIDX_BYTE_OID_VERSION=5
181182
MIDX_BYTE_CHUNK_COUNT=6
@@ -192,6 +193,10 @@ MIDX_OID_FANOUT_WIDTH=4
192193
MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + 1))
193194
MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
194195
MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
196+
MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
197+
MIDX_OFFSET_WIDTH=8
198+
MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
199+
MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
195200

196201
test_expect_success 'verify bad version' '
197202
corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
@@ -243,6 +248,16 @@ test_expect_success 'verify oid lookup out of order' '
243248
"oid lookup out of order"
244249
'
245250

251+
test_expect_success 'verify incorrect pack-int-id' '
252+
corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
253+
"bad pack-int-id"
254+
'
255+
256+
test_expect_success 'verify incorrect offset' '
257+
corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
258+
"incorrect object offset"
259+
'
260+
246261
test_expect_success 'repack removes multi-pack-index' '
247262
test_path_is_file $objdir/pack/multi-pack-index &&
248263
git repack -adf &&
@@ -310,4 +325,16 @@ test_expect_success 'verify multi-pack-index with 64-bit offsets' '
310325
git multi-pack-index verify --object-dir=objects64
311326
'
312327

328+
NUM_OBJECTS=63
329+
MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
330+
MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
331+
MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
332+
MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
333+
MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
334+
335+
test_expect_success 'verify incorrect 64-bit offset' '
336+
corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
337+
"incorrect object offset"
338+
'
339+
313340
test_done

0 commit comments

Comments
 (0)