@matheusbsbs / matheus-bagliano / commits / 797bd6f490
Fork 0

check_packed_git_idx(): check integrity of the idx file itself.

Although pack-check.c had routine to verify the checksum for the pack index file itself, the core did not check it before using it. This is stolen from the patch to tighten packname requirements. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 22, 2005 at 22:57 UTC 797bd6f490c91c07986382b9f268e0df712cb246
2 files changed +22 -1
sha1_file.c
+15 -1
@@ -321,12 +321,16 @@ struct packed_git *packed_git;
321 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
322 void **idx_map_)
323 {
324 + SHA_CTX ctx;
325 + unsigned char sha1[20];
326 void *idx_map;
327 unsigned int *index;
328 unsigned long idx_size;
329 int nr, i;
328 - int fd = open(path, O_RDONLY);
330 + int fd;
331 struct stat st;
332 +
333 + fd = open(path, O_RDONLY);
334 if (fd < 0)
335 return -1;
336 if (fstat(fd, &st)) {
@@ -364,6 +368,16 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
368 if (idx_size != 4*256 + nr * 24 + 20 + 20)
369 return error("wrong index file size");
370
371 + /*
372 + * File checksum.
373 + */
374 + SHA1_Init(&ctx);
375 + SHA1_Update(&ctx, idx_map, idx_size-20);
376 + SHA1_Final(sha1, &ctx);
377 +
378 + if (memcmp(sha1, idx_map + idx_size - 20, 20))
379 + return error("index checksum mismatch");
380 +
381 return 0;
382 }
383
t/t5300-pack-object.sh
+7
@@ -163,6 +163,13 @@ test_expect_success \
163 else :;
164 fi &&
165
166 + cp test-1-${packname_1}.pack test-3.pack &&
167 + dd if=/dev/zero of=test-3.idx count=1 bs=1 conv=notrunc seek=1200 &&
168 + if git-verify-pack test-3.pack
169 + then false
170 + else :;
171 + fi &&
172 +
173 :'
174
175 test_expect_success \