csum-file: provide a function to release checkpoints

A hashfile_checkpoint struct is basically just a copy of the hash_ctx state at a given point in the file. As such, it contains its own git_hash_ctx which may (depending on the underlying hash implementation) need to be discarded when we're done with it. Let's add a "release" function which cleans up the hash context it holds. I chose "release" here and not "discard" because you'd use this to clean up every checkpoint, whether you used it or not. As opposed to git_hash_discard(), which is needed only if you didn't call git_hash_final(). There are only two callers which use hashfile_checkpoints, and we can add release calls to both. When built with "SANITIZE=leak OPENSSL_SHA1_UNSAFE=1", this makes both t1050 and t9300 leak-free. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 2, 2026 at 04:03 UTC 46ba44e1fd6b1a3d71d529f6713821efc26072c9
4 files changed +9
builtin/fast-import.c
+1
@@ -1214,6 +1214,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
1214 out:
1215 free(in_buf);
1216 free(out_buf);
1217 + hashfile_checkpoint_release(&checkpoint);
1218 }
1219
1220 /* All calls must be guarded by find_object() or find_mark() to
csum-file.c
+5
@@ -223,6 +223,11 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint
223 return 0;
224 }
225
226 +void hashfile_checkpoint_release(struct hashfile_checkpoint *checkpoint)
227 +{
228 + git_hash_discard(&checkpoint->ctx);
229 +}
230 +
231 void crc32_begin(struct hashfile *f)
232 {
233 f->crc32 = crc32(0, NULL, 0);
csum-file.h
+1
@@ -39,6 +39,7 @@ struct hashfile_checkpoint {
39 void hashfile_checkpoint_init(struct hashfile *, struct hashfile_checkpoint *);
40 void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
41 int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
42 +void hashfile_checkpoint_release(struct hashfile_checkpoint *);
43
44 /* finalize_hashfile flags */
45 #define CSUM_CLOSE 1
object-file.c
+2
@@ -1639,6 +1639,8 @@ static int index_blob_packfile_transaction(struct odb_transaction_files *transac
1639 state->alloc_written);
1640 state->written[state->nr_written++] = idx;
1641 }
1642 +
1643 + hashfile_checkpoint_release(&checkpoint);
1644 return 0;
1645 }
1646