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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 13, 2018 at 11:02 UTC cc6af73c029da23f7b2c98c60e4fba1ca2db215b
2 files changed +55 -1
midx.c
+28 -1
@@ -197,7 +197,8 @@ int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id)
197 struct strbuf pack_name = STRBUF_INIT;
198
199 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);
202
203 if (m->packs[pack_int_id])
204 return 0;
@@ -970,5 +971,31 @@ int verify_midx_file(const char *object_dir)
971 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
972 }
973
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 +
1000 return verify_midx_error;
1001 }
t/t5319-multi-pack-index.sh
+27
@@ -176,6 +176,7 @@ test_expect_success 'verify bad signature' '
176 '
177
178 HASH_LEN=20
179 +NUM_OBJECTS=74
180 MIDX_BYTE_VERSION=4
181 MIDX_BYTE_OID_VERSION=5
182 MIDX_BYTE_CHUNK_COUNT=6
@@ -192,6 +193,10 @@ MIDX_OID_FANOUT_WIDTH=4
193 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + 1))
194 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
195 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))
200
201 test_expect_success 'verify bad version' '
202 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
@@ -243,6 +248,16 @@ test_expect_success 'verify oid lookup out of order' '
248 "oid lookup out of order"
249 '
250
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 +
261 test_expect_success 'repack removes multi-pack-index' '
262 test_path_is_file $objdir/pack/multi-pack-index &&
263 git repack -adf &&
@@ -310,4 +325,16 @@ test_expect_success 'verify multi-pack-index with 64-bit offsets' '
325 git multi-pack-index verify --object-dir=objects64
326 '
327
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 +
340 test_done