Skip to content

Commit 5f658d1

Browse files
derrickstoleegitster
authored andcommitted
fsck: check rev-index position values
When checking a rev-index file, it may be helpful to identify exactly which positions are incorrect. Compare the rev-index to a freshly-computed in-memory rev-index and report the comparison failures. This additional check (on top of the checksum validation) can help find files that were corrupt by a single bit flip on-disk or perhaps were written incorrectly due to a bug in Git. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d975fe1 commit 5f658d1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

pack-revindex.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,33 @@ int load_pack_revindex(struct repository *r, struct packed_git *p)
310310
*/
311311
int verify_pack_revindex(struct packed_git *p)
312312
{
313+
int res = 0;
314+
313315
/* Do not bother checking if not initialized. */
314-
if (!p->revindex_map)
315-
return 0;
316+
if (!p->revindex_map || !p->revindex_data)
317+
return res;
316318

317319
if (!hashfile_checksum_valid((const unsigned char *)p->revindex_map, p->revindex_size)) {
318320
error(_("invalid checksum"));
319-
return -1;
321+
res = -1;
320322
}
321323

322-
return 0;
324+
/* This may fail due to a broken .idx. */
325+
if (create_pack_revindex_in_memory(p))
326+
return res;
327+
328+
for (size_t i = 0; i < p->num_objects; i++) {
329+
uint32_t nr = p->revindex[i].nr;
330+
uint32_t rev_val = get_be32(p->revindex_data + i);
331+
332+
if (nr != rev_val) {
333+
error(_("invalid rev-index position at %"PRIu64": %"PRIu32" != %"PRIu32""),
334+
(uint64_t)i, nr, rev_val);
335+
res = -1;
336+
}
337+
}
338+
339+
return res;
323340
}
324341

325342
int load_midx_revindex(struct multi_pack_index *m)

t/t5325-reverse-index.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,9 @@ test_expect_success 'fsck catches invalid checksum' '
185185
"invalid checksum"
186186
'
187187

188+
test_expect_success 'fsck catches invalid row position' '
189+
corrupt_rev_and_verify 14 "\07" \
190+
"invalid rev-index position"
191+
'
192+
188193
test_done

0 commit comments

Comments
 (0)