[PATCH 0/9] hash algorithm leak fixes
From: Jeff King <hidden>
Date: 2026-07-02 07:52:36
This series fixes some leaks you can find by running:
make SANITIZE=leak \
OPENSSL_SHA256=1 \
GIT_TEST_DEFAULT_HASH=sha256 \
test
The crux of the issue is that we depend on calling git_hash_final() to
clean up any git_hash_ctx we've initialized. But we don't always call
that function (we may return early due to an error, etc).
We don't see these in our regular leak-test builds because the default
hash implementations we use treat the hash_ctx as a sequence of bytes.
So there's no cleanup needed, and just letting the context go out of
scope is fine. But other implementations do allocate on initialization,
and need to have some kind of free/discard function. So building with
OPENSSL_SHA256 above is what lets us see the leaks.
You can see the same thing with OPENSSL_SHA1, but of course we don't
recommend that. Using OPENSSL_SHA1_UNSAFE likewise, but it sees only a
subset of the leaks since it is only used in a few code paths. Those
leaks would be found if we turned on leak-checking in the
linux-TEST-vars job, but the rest of them would require leak-checking
the linux-sha256 job.
And as a special bonus, patch 8 is a semi-related leak that only affects
libgcrypt. I don't think we build against that in CI at all. :-/
[1/9]: csum-file: drop discard_hashfile()
[2/9]: hash: add discard primitive
[3/9]: csum-file: always finalize or discard hash
[4/9]: csum-file: provide a function to release checkpoints
[5/9]: patch-id: discard hash when done
[6/9]: check_stream_oid(): discard hash on read error
[7/9]: http: discard hash in dumb-http http_object_request
[8/9]: hash: fix memory leak copying sha256 gcrypt handles
[9/9]: hash: add platform-specific discard functions
builtin/fast-import.c | 1 +
builtin/patch-id.c | 1 +
csum-file.c | 30 +++++++++++++++++-------------
csum-file.h | 2 +-
diff.c | 1 +
hash.c | 29 +++++++++++++++++++++++++++++
hash.h | 22 ++++++++++++++++++++++
http.c | 4 ++++
http.h | 1 +
object-file.c | 4 ++++
sha1/openssl.h | 6 ++++++
sha256/gcrypt.h | 7 +++++++
sha256/openssl.h | 6 ++++++
13 files changed, 100 insertions(+), 14 deletions(-)
-Peff