git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH 07/11] merge-file: fix memory leaks on error path
Date: Thu, 30 Jun 2022 20:00:17 +0200	[thread overview]
Message-ID: <patch-07.11-8803a0df799-20220630T175714Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-00.11-00000000000-20220630T175714Z-avarab@gmail.com>

Fix a memory leak in "merge-file", we need to loop over the "mmfs"
array and free() what we've got so far when we error out. As a result
we can mark a test as passing with SANITIZE=leak using
"TEST_PASSES_SANITIZE_LEAK=true".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/merge-file.c  | 10 ++++++----
 t/t6403-merge-file.sh |  2 ++
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/builtin/merge-file.c b/builtin/merge-file.c
index 793817f3cb9..c923bbf2abb 100644
--- a/builtin/merge-file.c
+++ b/builtin/merge-file.c
@@ -87,7 +87,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 
 		free(fname);
 		if (ret)
-			return ret;
+			goto cleanup;
+
 	}
 
 	xmp.ancestor = names[1];
@@ -95,9 +96,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 	xmp.file2 = names[2];
 	ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result);
 
-	for (i = 0; i < 3; i++)
-		free(mmfs[i].ptr);
-
 	if (ret >= 0) {
 		const char *filename = argv[0];
 		char *fpath = prefix_filename(prefix, argv[0]);
@@ -118,5 +116,9 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 	if (ret > 127)
 		ret = 127;
 
+cleanup:
+	for (i = 0; i < 3; i++)
+		free(mmfs[i].ptr);
+
 	return ret;
 }
diff --git a/t/t6403-merge-file.sh b/t/t6403-merge-file.sh
index 2f421d967ab..1a7082323dd 100755
--- a/t/t6403-merge-file.sh
+++ b/t/t6403-merge-file.sh
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='RCS merge replacement: merge-file'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-- 
2.37.0.874.g7d3439f13c4


  parent reply	other threads:[~2022-06-30 18:00 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-30 18:00 [PATCH 00/11] built-ins: fix common memory leaks Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 01/11] check-ref-format: fix trivial memory leak Ævar Arnfjörð Bjarmason
2022-06-30 21:55   ` Junio C Hamano
2022-06-30 18:00 ` [PATCH 02/11] clone: fix memory leak in copy_ref() call Ævar Arnfjörð Bjarmason
2022-06-30 22:03   ` Junio C Hamano
2022-06-30 18:00 ` [PATCH 03/11] submodule.c: free() memory from xgetcwd() Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 04/11] revert: free "struct replay_opts" members Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 05/11] cat-file: fix a memory leak in --batch-command mode Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 06/11] merge-file: refactor for subsequent memory leak fix Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` Ævar Arnfjörð Bjarmason [this message]
2022-06-30 18:00 ` [PATCH 08/11] checkout: add a missing clear_unpack_trees_porcelain() Ævar Arnfjörð Bjarmason
2022-06-30 22:20   ` Junio C Hamano
2022-06-30 23:34     ` Ævar Arnfjörð Bjarmason
2022-06-30 23:45       ` Junio C Hamano
2022-06-30 18:00 ` [PATCH 09/11] gc: fix a memory leak Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 10/11] cat-file: fix a common "struct object_context" " Ævar Arnfjörð Bjarmason
2022-06-30 18:00 ` [PATCH 11/11] pull: fix a "struct oid_array" " Ævar Arnfjörð Bjarmason
2022-07-01 10:42 ` [PATCH v2 00/11] built-ins: fix common memory leaks Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 01/11] check-ref-format: fix trivial memory leak Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 02/11] clone: fix memory leak in wanted_peer_refs() Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 03/11] submodule.c: free() memory from xgetcwd() Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 04/11] revert: free "struct replay_opts" members Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 05/11] cat-file: fix a memory leak in --batch-command mode Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 06/11] merge-file: refactor for subsequent memory leak fix Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 07/11] merge-file: fix memory leaks on error path Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 08/11] checkout: avoid "struct unpack_trees_options" leak Ævar Arnfjörð Bjarmason
2022-07-01 20:25     ` Junio C Hamano
2022-07-01 10:42   ` [PATCH v2 09/11] gc: fix a memory leak Ævar Arnfjörð Bjarmason
2022-07-01 10:42   ` [PATCH v2 10/11] cat-file: fix a common "struct object_context" " Ævar Arnfjörð Bjarmason
2022-07-01 10:43   ` [PATCH v2 11/11] pull: fix a "struct oid_array" " Ævar Arnfjörð Bjarmason
2022-07-01 18:58   ` [PATCH v2 00/11] built-ins: fix common memory leaks Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=patch-07.11-8803a0df799-20220630T175714Z-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).