Skip to content

Commit a8dd382

Browse files
ttaylorrgitster
authored andcommitted
csum-file: introduce hashfile_checkpoint_init()
In 106140a (builtin/fast-import: fix segfault with unsafe SHA1 backend, 2024-12-30) and 9218c0b (bulk-checkin: fix segfault with unsafe SHA1 backend, 2024-12-30), we observed the effects of failing to initialize a hashfile_checkpoint with the same hash function implementation as is used by the hashfile it is used to checkpoint. While both 106140a and 9218c0b work around the immediate crash, changing the hash function implementation within the hashfile API to, for example, the non-unsafe variant would re-introduce the crash. This is a result of the tight coupling between initializing hashfiles and hashfile_checkpoints. Introduce and use a new function which ensures that both parts of a hashfile and hashfile_checkpoint pair use the same hash function implementation to avoid such crashes. A few things worth noting: - In the change to builtin/fast-import.c::stream_blob(), we can see that by removing the explicit reference to 'the_hash_algo->unsafe_init_fn()', we are hardened against the hashfile API changing away from the_hash_algo (or its unsafe variant) in the future. - The bulk-checkin code no longer needs to explicitly zero-initialize the hashfile_checkpoint, since it is now done as a result of calling 'hashfile_checkpoint_init()'. - Also in the bulk-checkin code, we add an additional call to prepare_to_stream() outside of the main loop in order to initialize 'state->f' so we know which hash function implementation to use when calling 'hashfile_checkpoint_init()'. This is OK, since subsequent 'prepare_to_stream()' calls are noops. However, we only need to call 'prepare_to_stream()' when we have the HASH_WRITE_OBJECT bit set in our flags. Without that bit, calling 'prepare_to_stream()' does not assign 'state->f', so we have nothing to initialize. - Other uses of the 'checkpoint' in 'deflate_blob_to_pack()' are appropriately guarded. Helped-by: Patrick Steinhardt <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3339180 commit a8dd382

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

builtin/fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11061106
|| (pack_size + PACK_SIZE_THRESHOLD + len) < pack_size)
11071107
cycle_packfile();
11081108

1109-
the_hash_algo->unsafe_init_fn(&checkpoint.ctx);
1109+
hashfile_checkpoint_init(pack_file, &checkpoint);
11101110
hashfile_checkpoint(pack_file, &checkpoint);
11111111
offset = checkpoint.offset;
11121112

bulk-checkin.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
261261
git_hash_ctx ctx;
262262
unsigned char obuf[16384];
263263
unsigned header_len;
264-
struct hashfile_checkpoint checkpoint = {0};
264+
struct hashfile_checkpoint checkpoint;
265265
struct pack_idx_entry *idx = NULL;
266266

267267
seekback = lseek(fd, 0, SEEK_CUR);
@@ -272,12 +272,15 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
272272
OBJ_BLOB, size);
273273
the_hash_algo->init_fn(&ctx);
274274
the_hash_algo->update_fn(&ctx, obuf, header_len);
275-
the_hash_algo->unsafe_init_fn(&checkpoint.ctx);
276275

277276
/* Note: idx is non-NULL when we are writing */
278-
if ((flags & HASH_WRITE_OBJECT) != 0)
277+
if ((flags & HASH_WRITE_OBJECT) != 0) {
279278
CALLOC_ARRAY(idx, 1);
280279

280+
prepare_to_stream(state, flags);
281+
hashfile_checkpoint_init(state->f, &checkpoint);
282+
}
283+
281284
already_hashed_to = 0;
282285

283286
while (1) {

csum-file.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp
206206
return hashfd_internal(fd, name, tp, 8 * 1024);
207207
}
208208

209+
void hashfile_checkpoint_init(struct hashfile *f,
210+
struct hashfile_checkpoint *checkpoint)
211+
{
212+
memset(checkpoint, 0, sizeof(*checkpoint));
213+
f->algop->init_fn(&checkpoint->ctx);
214+
}
215+
209216
void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
210217
{
211218
hashflush(f);

csum-file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct hashfile_checkpoint {
3636
git_hash_ctx ctx;
3737
};
3838

39+
void hashfile_checkpoint_init(struct hashfile *, struct hashfile_checkpoint *);
3940
void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
4041
int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
4142

0 commit comments

Comments
 (0)