Skip to content

Commit b7429e1

Browse files
committed
midx: use a byte array for checksum
1 parent 0e53e55 commit b7429e1

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/midx.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ int git_midx_parse(
178178
struct git_midx_chunk *last_chunk;
179179
uint32_t i;
180180
off64_t last_chunk_offset, chunk_offset, trailer_offset;
181-
git_oid idx_checksum = {{0}};
181+
size_t checksum_size;
182+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
182183
int error;
183184
struct git_midx_chunk chunk_packfile_names = {0},
184185
chunk_oid_fanout = {0},
@@ -208,14 +209,17 @@ int git_midx_parse(
208209
last_chunk_offset =
209210
sizeof(struct git_midx_header) +
210211
(1 + hdr->chunks) * 12;
211-
trailer_offset = size - GIT_OID_RAWSZ;
212+
213+
checksum_size = GIT_HASH_SHA1_SIZE;
214+
trailer_offset = size - checksum_size;
215+
212216
if (trailer_offset < last_chunk_offset)
213217
return midx_error("wrong index size");
214-
git_oid_cpy(&idx->checksum, (git_oid *)(data + trailer_offset));
218+
memcpy(idx->checksum, data + trailer_offset, checksum_size);
215219

216-
if (git_hash_buf(idx_checksum.id, data, (size_t)trailer_offset, GIT_HASH_ALGORITHM_SHA1) < 0)
220+
if (git_hash_buf(checksum, data, (size_t)trailer_offset, GIT_HASH_ALGORITHM_SHA1) < 0)
217221
return midx_error("could not calculate signature");
218-
if (!git_oid_equal(&idx_checksum, &idx->checksum))
222+
if (memcmp(checksum, idx->checksum, checksum_size) != 0)
219223
return midx_error("index signature mismatch");
220224

221225
chunk_hdr = data + sizeof(struct git_midx_header);
@@ -341,7 +345,8 @@ bool git_midx_needs_refresh(
341345
git_file fd = -1;
342346
struct stat st;
343347
ssize_t bytes_read;
344-
git_oid idx_checksum = {{0}};
348+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
349+
size_t checksum_size;
345350

346351
/* TODO: properly open the file without access time using O_NOATIME */
347352
fd = git_futils_open_ro(path);
@@ -360,13 +365,14 @@ bool git_midx_needs_refresh(
360365
return true;
361366
}
362367

363-
bytes_read = p_pread(fd, &idx_checksum, GIT_OID_RAWSZ, st.st_size - GIT_OID_RAWSZ);
368+
checksum_size = GIT_HASH_SHA1_SIZE;
369+
bytes_read = p_pread(fd, checksum, checksum_size, st.st_size - GIT_OID_RAWSZ);
364370
p_close(fd);
365371

366-
if (bytes_read != GIT_OID_RAWSZ)
372+
if (bytes_read != (ssize_t)checksum_size)
367373
return true;
368374

369-
return !git_oid_equal(&idx_checksum, &idx->checksum);
375+
return (memcmp(checksum, idx->checksum, checksum_size) != 0);
370376
}
371377

372378
int git_midx_entry_find(
@@ -653,7 +659,8 @@ static int midx_write(
653659
oid_lookup = GIT_STR_INIT,
654660
object_offsets = GIT_STR_INIT,
655661
object_large_offsets = GIT_STR_INIT;
656-
git_oid idx_checksum = {{0}};
662+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
663+
size_t checksum_size;
657664
git_midx_entry *entry;
658665
object_entry_array_t object_entries_array = GIT_ARRAY_INIT;
659666
git_vector object_entries = GIT_VECTOR_INIT;
@@ -669,6 +676,7 @@ static int midx_write(
669676
hash_cb_data.cb_data = cb_data;
670677
hash_cb_data.ctx = &ctx;
671678

679+
checksum_size = GIT_HASH_SHA1_SIZE;
672680
error = git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1);
673681
if (error < 0)
674682
return error;
@@ -820,10 +828,10 @@ static int midx_write(
820828
goto cleanup;
821829

822830
/* Finalize the checksum and write the trailer. */
823-
error = git_hash_final(idx_checksum.id, &ctx);
831+
error = git_hash_final(checksum, &ctx);
824832
if (error < 0)
825833
goto cleanup;
826-
error = write_cb((const char *)&idx_checksum, sizeof(idx_checksum), cb_data);
834+
error = write_cb((char *)checksum, checksum_size, cb_data);
827835
if (error < 0)
828836
goto cleanup;
829837

src/midx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ typedef struct git_midx_file {
5151
size_t num_object_large_offsets;
5252

5353
/* The trailer of the file. Contains the SHA1-checksum of the whole file. */
54-
git_oid checksum;
54+
unsigned char checksum[GIT_HASH_SHA1_SIZE];
5555

5656
/* something like ".git/objects/pack/multi-pack-index". */
5757
git_str filename;

0 commit comments

Comments
 (0)