Subject: [PATCH v3 2/2] reftable/writer: fix memory leak when `writer_index_hash()` fails

From: "Lidong Yan via GitGitGadget" <[email-masked]>
Date: Mon, 12 May 2025 12:49:04 +0000

From: Lidong Yan <[email-masked]>

In reftable/writer.c:writer_index_hash(), if `reftable_buf_add` failed,
key allocated by `reftable_malloc` will not be insert into `obj_index_tree`
thus leaks. Simple add reftable_free(key) will solve this problem.

Signed-off-by: Lidong Yan <[email-masked]>
---
 reftable/writer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/reftable/writer.c b/reftable/writer.c
index 3ceb37428887..3b4ebdd6dced 100644
--- a/reftable/writer.c
+++ b/reftable/writer.c
@@ -258,8 +258,10 @@ static int writer_index_hash(struct reftable_writer *w, struct reftable_buf *has
 
 		reftable_buf_reset(&key->hash);
 		err = reftable_buf_add(&key->hash, hash->buf, hash->len);
-		if (err < 0)
+		if (err < 0) {
+			reftable_free(key);
 			return err;
+		}
 		tree_insert(&w->obj_index_tree, key,
 			    &obj_index_tree_node_compare);
 	} else {
-- 
gitgitgadget