Skip to content

Commit 33bbc59

Browse files
peffgitster
authored andcommitted
fsck: correctly compute checksums on idx files larger than 4GB
When checking the trailing checksum hash of a .idx file, we pass the whole buffer (minus the trailing hash) into a single call to the_hash_algo->update_fn(). But we cast it to an "unsigned int". This comes from c4001d9 (Use off_t when we really mean a file offset., 2007-03-06). That commit started storing the index_size variable as an off_t, but our mozilla-sha1 implementation from the time was limited to a smaller size. Presumably the cast was a way of annotating that we expected .idx files to be small, and so we didn't need to loop (as we do for arbitrarily-large .pack files). Though as an aside it was still wrong, because the mozilla function actually took a signed int. These days our hash-update functions are defined to take a size_t, so we can pass the whole buffer in directly. The cast is actually causing a buggy truncation! While we're here, though, let's drop the confusing off_t variable in the first place. We're getting the size not from the filesystem anyway, but from p->index_size, which is a size_t. In fact, we can make the code a bit more readable by dropping our local variable duplicating p->index_size, and instead have one that stores the size of the actual index data, minus the trailing hash. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a9bc372 commit 33bbc59

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

pack-check.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,22 @@ static int verify_packfile(struct repository *r,
164164

165165
int verify_pack_index(struct packed_git *p)
166166
{
167-
off_t index_size;
167+
size_t len;
168168
const unsigned char *index_base;
169169
git_hash_ctx ctx;
170170
unsigned char hash[GIT_MAX_RAWSZ];
171171
int err = 0;
172172

173173
if (open_pack_index(p))
174174
return error("packfile %s index not opened", p->pack_name);
175-
index_size = p->index_size;
176175
index_base = p->index_data;
176+
len = p->index_size - the_hash_algo->rawsz;
177177

178178
/* Verify SHA1 sum of the index file */
179179
the_hash_algo->init_fn(&ctx);
180-
the_hash_algo->update_fn(&ctx, index_base, (unsigned int)(index_size - the_hash_algo->rawsz));
180+
the_hash_algo->update_fn(&ctx, index_base, len);
181181
the_hash_algo->final_fn(hash, &ctx);
182-
if (!hasheq(hash, index_base + index_size - the_hash_algo->rawsz))
182+
if (!hasheq(hash, index_base + len))
183183
err = error("Packfile index for %s hash mismatch",
184184
p->pack_name);
185185
return err;

0 commit comments

Comments
 (0)