Skip to content

Commit 336f98f

Browse files
derrickstoleeKevin Willford
authored andcommitted
midx: verify checksum footer
Signed-off-by: Derrick Stolee <[email protected]>
1 parent a489f2a commit 336f98f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

midx.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,9 @@ int midx_verify(const char *pack_dir, const char *midx_id)
952952
uint32_t i, cur_fanout_pos = 0;
953953
struct midxed_git *m;
954954
const char *midx_head_path;
955-
struct object_id cur_oid, prev_oid;
955+
struct object_id cur_oid, prev_oid, checksum;
956+
struct hashfile *f;
957+
int devnull, checksum_fail = 0;
956958

957959
if (midx_id) {
958960
size_t sz;
@@ -970,6 +972,17 @@ int midx_verify(const char *pack_dir, const char *midx_id)
970972
goto cleanup;
971973
}
972974

975+
976+
devnull = open("/dev/null", O_WRONLY);
977+
f = hashfd(devnull, NULL);
978+
hashwrite(f, m->data, m->data_len - m->hdr->hash_len);
979+
finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
980+
if (hashcmp(checksum.hash, m->data + m->data_len - m->hdr->hash_len)) {
981+
midx_report(_("the midx file has incorrect checksum and is likely corrupt"));
982+
verify_midx_error = 0;
983+
checksum_fail = 1;
984+
}
985+
973986
if (m->hdr->hash_version != MIDX_OID_VERSION)
974987
midx_report("invalid hash version");
975988
if (m->hdr->hash_len != MIDX_OID_LEN)
@@ -987,8 +1000,6 @@ int midx_verify(const char *pack_dir, const char *midx_id)
9871000
goto cleanup;
9881001

9891002
for (i = 0; i < m->num_packs; i++) {
990-
fprintf(stderr, "preparing %s\n", m->pack_names[i]);
991-
fflush(stderr);
9921003
if (prepare_midx_pack(m, i)) {
9931004
midx_report("failed to prepare pack %s",
9941005
m->pack_names[i]);
@@ -1060,5 +1071,5 @@ int midx_verify(const char *pack_dir, const char *midx_id)
10601071
if (m)
10611072
close_midx(m);
10621073
free(m);
1063-
return verify_midx_error;
1074+
return verify_midx_error | checksum_fail;
10641075
}

t/t5319-midx.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ MIDX_BYTE_OBJECT_OFFSET=`expr $MIDX_OFFSET_OBJECT_OFFSETS + \
262262
MIDX_OFFSET_PACKFILE_NAMES=`expr $MIDX_OFFSET_OBJECT_OFFSETS + \
263263
$MIDX_WIDTH_OBJECT_OFFSETS \* $MIDX_NUM_OBJECTS`
264264
MIDX_BYTE_PACKFILE_NAMES=`expr $MIDX_OFFSET_PACKFILE_NAMES + 10`
265+
MIDX_LEN=$(stat --printf="%s" midx-*.midx)
266+
MIDX_BYTE_CHECKSUM=`expr $MIDX_LEN - $HASH_LEN`
265267

266268
test_expect_success 'midx --verify succeeds' '
267269
git midx --verify --pack-dir .
@@ -369,6 +371,11 @@ test_expect_success 'verify packfile lookup' '
369371
"invalid packfile name lookup"
370372
'
371373

374+
test_expect_success 'verify checksum hash' '
375+
corrupt_midx_and_verify $MIDX_BYTE_CHECKSUM "\00" \
376+
"incorrect checksum"
377+
'
378+
372379
# usage: corrupt_data <file> <pos> [<data>]
373380
corrupt_data() {
374381
file=$1

0 commit comments

Comments
 (0)