Subject: [PATCH v3 0/2] reftable/writer: fix memory leak when write fails

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

Lidong Yan (2):
  reftable/writer: fix memory leak when `padded_write()` fails
  reftable/writer: fix memory leak when `writer_index_hash()` fails

 reftable/writer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1958%2Fbrandb97%2Ffix-reftable-padded-write-leak-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1958/brandb97/fix-reftable-padded-write-leak-v3
Pull-Request: https://github.com/git/git/pull/1958

Range-diff vs v2:

 1:  9329ae2d478 ! 1:  13ebdd672ff reftable/writer: fix memory leak if write fails
     @@ Metadata
      Author: Lidong Yan <[email-masked]>
      
       ## Commit message ##
     -    reftable/writer: fix memory leak if write fails
     +    reftable/writer: fix memory leak when `padded_write()` fails
      
          In reftable/writer.c:padded_write(), if w->writer failed, zeroed
          allocated in `reftable_calloc` will leak. w->writer could be
     @@ Commit message
          some write error. Simply add reftable_free(zeroed) will solve this
          problem.
      
     -    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 ##
     @@ reftable/writer.c: static int padded_write(struct reftable_writer *w, uint8_t *d
       
       		w->pending_padding = 0;
       		reftable_free(zeroed);
     -@@ reftable/writer.c: 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 {
 -:  ----------- > 2:  64f778ce2ba reftable/writer: fix memory leak when `writer_index_hash()` fails

-- 
gitgitgadget