Skip to content

Commit a44a5c0

Browse files
committed
Merge branch 'np/pack-check'
* np/pack-check: make verify-pack a bit more useful with bad packs
2 parents 310b9de + 6241360 commit a44a5c0

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

pack-check.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ static int verify_packfile(struct packed_git *p,
2525
off_t index_size = p->index_size;
2626
const unsigned char *index_base = p->index_data;
2727
SHA_CTX ctx;
28-
unsigned char sha1[20];
29-
off_t offset = 0, pack_sig = p->pack_size - 20;
28+
unsigned char sha1[20], *pack_sig;
29+
off_t offset = 0, pack_sig_ofs = p->pack_size - 20;
3030
uint32_t nr_objects, i;
31-
int err;
31+
int err = 0;
3232
struct idx_entry *entries;
3333

3434
/* Note that the pack header checks are actually performed by
@@ -38,21 +38,22 @@ static int verify_packfile(struct packed_git *p,
3838
*/
3939

4040
SHA1_Init(&ctx);
41-
while (offset < pack_sig) {
41+
while (offset < pack_sig_ofs) {
4242
unsigned int remaining;
4343
unsigned char *in = use_pack(p, w_curs, offset, &remaining);
4444
offset += remaining;
45-
if (offset > pack_sig)
46-
remaining -= (unsigned int)(offset - pack_sig);
45+
if (offset > pack_sig_ofs)
46+
remaining -= (unsigned int)(offset - pack_sig_ofs);
4747
SHA1_Update(&ctx, in, remaining);
4848
}
4949
SHA1_Final(sha1, &ctx);
50-
if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL)))
51-
return error("Packfile %s SHA1 mismatch with itself",
52-
p->pack_name);
53-
if (hashcmp(sha1, index_base + index_size - 40))
54-
return error("Packfile %s SHA1 mismatch with idx",
55-
p->pack_name);
50+
pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
51+
if (hashcmp(sha1, pack_sig))
52+
err = error("%s SHA1 checksum mismatch",
53+
p->pack_name);
54+
if (hashcmp(index_base + index_size - 40, pack_sig))
55+
err = error("%s SHA1 does not match its inddex",
56+
p->pack_name);
5657
unuse_pack(w_curs);
5758

5859
/* Make sure everything reachable from idx is valid. Since we
@@ -72,22 +73,23 @@ static int verify_packfile(struct packed_git *p,
7273
}
7374
qsort(entries, nr_objects, sizeof(*entries), compare_entries);
7475

75-
for (i = 0, err = 0; i < nr_objects; i++) {
76+
for (i = 0; i < nr_objects; i++) {
7677
void *data;
7778
enum object_type type;
7879
unsigned long size;
7980

8081
data = unpack_entry(p, entries[i].offset, &type, &size);
8182
if (!data) {
82-
err = error("cannot unpack %s from %s",
83-
sha1_to_hex(entries[i].sha1), p->pack_name);
84-
continue;
83+
err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
84+
sha1_to_hex(entries[i].sha1), p->pack_name,
85+
(uintmax_t)entries[i].offset);
86+
break;
8587
}
8688
if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
8789
err = error("packed %s from %s is corrupt",
8890
sha1_to_hex(entries[i].sha1), p->pack_name);
8991
free(data);
90-
continue;
92+
break;
9193
}
9294
free(data);
9395
}
@@ -158,37 +160,34 @@ int verify_pack(struct packed_git *p, int verbose)
158160
const unsigned char *index_base;
159161
SHA_CTX ctx;
160162
unsigned char sha1[20];
161-
int ret;
163+
int err = 0;
164+
struct pack_window *w_curs = NULL;
162165

163166
if (open_pack_index(p))
164167
return error("packfile %s index not opened", p->pack_name);
165168
index_size = p->index_size;
166169
index_base = p->index_data;
167170

168-
ret = 0;
169171
/* Verify SHA1 sum of the index file */
170172
SHA1_Init(&ctx);
171173
SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
172174
SHA1_Final(sha1, &ctx);
173175
if (hashcmp(sha1, index_base + index_size - 20))
174-
ret = error("Packfile index for %s SHA1 mismatch",
176+
err = error("Packfile index for %s SHA1 mismatch",
175177
p->pack_name);
176178

177-
if (!ret) {
178-
/* Verify pack file */
179-
struct pack_window *w_curs = NULL;
180-
ret = verify_packfile(p, &w_curs);
181-
unuse_pack(&w_curs);
182-
}
179+
/* Verify pack file */
180+
err |= verify_packfile(p, &w_curs);
181+
unuse_pack(&w_curs);
183182

184183
if (verbose) {
185-
if (ret)
184+
if (err)
186185
printf("%s: bad\n", p->pack_name);
187186
else {
188187
show_pack_info(p);
189188
printf("%s: ok\n", p->pack_name);
190189
}
191190
}
192191

193-
return ret;
192+
return err;
194193
}

0 commit comments

Comments
 (0)