git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/9] check_stream_oid(): discard hash on read error

From
Jeff King <peff@peff.net>
Date
Jul 2, 2026, 08:05 UTC
Message-ID
<20260702080503.GF2029434@coredump.intra.peff.net>
In-Reply-To
<20260702075234.GA1548258@coredump.intra.peff.net>

The happy path of check_stream_oid() is to initialize a hash, feed the loose object zlib stream into it, and then get the final result. But if we hit a zlib error or see extra cruft we'll bail early with an error.

Since we never call git_hash_final() in this cases, any resources held by the git_hash_ctx may be leaked. Our default hash algorithms don't allocate anything in the hash_ctx, but some implementations do. For example, running:

  make SANITIZE=leak \
       OPENSSL_SHA256=1 \
       GIT_TEST_DEFAULT_HASH=sha256 \
       test

will fail t1450, since it feeds corrupted objects that cause us to bail from check_stream_oid(). This patch fixes it by discarding the hash in those early return paths. Trying to jump to a common "out:" label is not worth it here, as we must _not_ discard a hash that was already fed to git_hash_final(). And the hash_ctx itself does not carry any information (so we cannot check for a NULL pointer, etc).

Signed-off-by: Jeff King <peff@peff.net>
---
 object-file.c | 2 ++
 1 file changed, 2 insertions(+)
diff --git a/object-file.c b/object-file.c
index 32a0d6d237..035d005279 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1587,11 +1587,13 @@ static int check_stream_oid(git_zstream *stream,
 
 	if (status != Z_STREAM_END) {
 		error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
+		git_hash_discard(&c);
 		return -1;
 	}
 	if (stream->avail_in) {
 		error(_("garbage at end of loose object '%s'"),
 		      oid_to_hex(expected_oid));
+		git_hash_discard(&c);
 		return -1;
 	}
 
-- 
2.55.0.418.g37da59dd42
Previous: Jeff KingNext: Jeff King
Message 12 of 20 in “hash algorithm leak fixes”
  1. 0/9 hash algorithm leak fixesJeff King, Jul 2, 2026
  2. 1/9 csum-file: drop discard_hashfile()Jeff King, Jul 2, 2026
  3. Junio C HamanoJul 2, 2026
  4. Jeff KingJul 2, 2026
  5. Patrick SteinhardtJul 3, 2026
  6. 2/9 hash: add discard primitiveJeff King, Jul 2, 2026
  7. Patrick SteinhardtJul 3, 2026
  8. 3/9 csum-file: always finalize or discard hashJeff King, Jul 2, 2026
  9. 4/9 csum-file: provide a function to release checkpointsJeff King, Jul 2, 2026
  10. Patrick SteinhardtJul 3, 2026
  11. 5/9 patch-id: discard hash when doneJeff King, Jul 2, 2026
  12. 6/9 check_stream_oid(): discard hash on read errorJeff King, Jul 2, 2026
  13. 7/9 http: discard hash in dumb-http http_object_requestJeff King, Jul 2, 2026
  14. Patrick SteinhardtJul 3, 2026
  15. brian m. carlsonJul 3, 2026
  16. Jeff KingJul 6, 2026
  17. Jeff KingJul 6, 2026
  18. Patrick SteinhardtJul 6, 2026
  19. 8/9 hash: fix memory leak copying sha256 gcrypt handlesJeff King, Jul 2, 2026
  20. 9/9 hash: add platform-specific discard functionsJeff King, Jul 2, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.