pack-check.c::verify_packfile(): don't run SHA-1 update on huge data
Running the SHA1_Update() on the whole packfile in a single call revealed an overflow problem we had in the SHA-1 implementation on POWER architecture some time ago, which was fixed with commit b47f509b (June 19, 2006). Other SHA-1 implementations may have a similar problem. The sliding mmap() series already makes chunked calls to SHA1_Update(), so this patch itself will become moot when it graduates to "master", but in the meantime, run the hash function in smaller chunks to prevent possible future problems. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 3, 2007 at 23:09 UTC
8977c110b5bbd230c28c727ddb85856067d55cfb
1 file changed
+15
-5
pack-check.c
+15
-5
@@ -1,16 +1,18 @@
1
#include "cache.h"
2
#include "pack.h"
3
4
+#define BATCH (1u<<20)
5
+
6
static int verify_packfile(struct packed_git *p)
7
{
8
unsigned long index_size = p->index_size;
9
void *index_base = p->index_base;
10
SHA_CTX ctx;
11
unsigned char sha1[20];
10
- unsigned long pack_size = p->pack_size;
11
- void *pack_base;
12
struct pack_header *hdr;
13
int nr_objects, err, i;
14
+ unsigned char *packdata;
15
+ unsigned long datasize;
16
17
/* Header consistency check */
18
hdr = p->pack_base;
@@ -25,11 +27,19 @@ static int verify_packfile(struct packed_git *p)
27
"while idx size expects %d", nr_objects,
28
num_packed_objects(p));
29
30
+ /* Check integrity of pack data with its SHA-1 checksum */
31
SHA1_Init(&ctx);
29
- pack_base = p->pack_base;
30
- SHA1_Update(&ctx, pack_base, pack_size - 20);
32
+ packdata = p->pack_base;
33
+ datasize = p->pack_size - 20;
34
+ while (datasize) {
35
+ unsigned long batch = (datasize < BATCH) ? datasize : BATCH;
36
+ SHA1_Update(&ctx, packdata, batch);
37
+ datasize -= batch;
38
+ packdata += batch;
39
+ }
40
SHA1_Final(sha1, &ctx);
32
- if (hashcmp(sha1, (unsigned char *)pack_base + pack_size - 20))
41
+
42
+ if (hashcmp(sha1, (unsigned char *)(p->pack_base) + p->pack_size - 20))
43
return error("Packfile %s SHA1 mismatch with itself",
44
p->pack_name);
45
if (hashcmp(sha1, (unsigned char *)index_base + index_size - 40))