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

[PATCH 2/3] sha256/gcrypt: fix memory leak with SHA-256 repos

From
EWEric Wong <e@80x24.org>
Date
Jul 31, 2023, 12:08 UTC
Message-ID
<20230731120808.1230210-3-e@80x24.org>
In-Reply-To
<20230731120808.1230210-1-e@80x24.org>

`gcry_md_open' needs to be paired with `gcry_md_close' to ensure resources are released. Since our internal APIs don't have separate close/release callbacks, sticking it into the finalization callback seems appropriate.

Building with SANITIZE=leak and running `git fsck' on a SHA-256 repository no longer reports leaks.

Signed-off-by: Eric Wong <e@80x24.org>
---
 sha256/gcrypt.h | 1 +
 1 file changed, 1 insertion(+)
diff --git a/sha256/gcrypt.h b/sha256/gcrypt.h
index 68cf6b6a54..1d06a778af 100644
--- a/sha256/gcrypt.h
+++ b/sha256/gcrypt.h
@@ -20,6 +20,7 @@ static inline void gcrypt_SHA256_Update(gcrypt_SHA256_CTX *ctx, const void *data
 static inline void gcrypt_SHA256_Final(unsigned char *digest, gcrypt_SHA256_CTX *ctx)
 {
 	memcpy(digest, gcry_md_read(*ctx, GCRY_MD_SHA256), SHA256_DIGEST_SIZE);
+	gcry_md_close(*ctx);
 }
 
 static inline void gcrypt_SHA256_Clone(gcrypt_SHA256_CTX *dst, const gcrypt_SHA256_CTX *src)
Previous: Eric WongNext: Eric Wong
Message 3 of 5 in “sha256/gcrypt fixes”
  1. 0/3 sha256/gcrypt fixesEric Wong, Jul 31, 2023
  2. 1/3 sha256/gcrypt: fix build with SANITIZE=leakEric Wong, Jul 31, 2023
  3. 2/3 sha256/gcrypt: fix memory leak with SHA-256 reposEric Wong, Jul 31, 2023
  4. 3/3 sha256/gcrypt: die on gcry_md_open failuresEric Wong, Jul 31, 2023
  5. Junio C HamanoJul 31, 2023

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.