All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rebase: use child_process_clear() to clean
@ 2024-03-22 10:35 Jeff King
  2024-03-22 17:21 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff King @ 2024-03-22 10:35 UTC (permalink / raw)
  To: git

In the run_am() function, we set up a child_process struct to run
"git-am", allocating memory for its args and env strvecs. These are
normally cleaned up when we call run_command(). But if we encounter
certain errors, we exit the function early and try to clean up ourselves
by clearing the am.args field. This leaks the "env" strvec.

We should use child_process_clear() instead, which covers both. And more
importantly, it future proofs us against the struct ever growing more
allocated fields.

These are unlikely errors to happen in practice, so they don't actually
trigger the leak sanitizer in the tests. But we can add a new test which
does exercise one of the paths (and fails SANITIZE=leak without this
patch).

Signed-off-by: Jeff King <peff@peff.net>
---
I noticed this when investigating a different leak, since that other one
caused format-patch to crash, hitting one of the error paths. ;)

The results of "git grep 'strvec_clear.*\.args'" imply the problem
doesn't exist elsewhere (arguably the hit in test-trace2.c should use
child_process_clear() to set a good example, but it's already a bit of
an oddball use of the struct).

Not sure if the test is overkill. It really does nothing useful in a
non-leak-checking build.

 builtin/rebase.c               | 6 +++---
 t/t3438-rebase-broken-files.sh | 9 +++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index e444ab102d..b9d0fb3269 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -610,7 +610,7 @@ static int run_am(struct rebase_options *opts)
 		status = error_errno(_("could not open '%s' for writing"),
 				     rebased_patches);
 		free(rebased_patches);
-		strvec_clear(&am.args);
+		child_process_clear(&am);
 		return status;
 	}
 
@@ -638,7 +638,7 @@ static int run_am(struct rebase_options *opts)
 		struct reset_head_opts ropts = { 0 };
 		unlink(rebased_patches);
 		free(rebased_patches);
-		strvec_clear(&am.args);
+		child_process_clear(&am);
 
 		ropts.oid = &opts->orig_head->object.oid;
 		ropts.branch = opts->head_name;
@@ -659,7 +659,7 @@ static int run_am(struct rebase_options *opts)
 		status = error_errno(_("could not open '%s' for reading"),
 				     rebased_patches);
 		free(rebased_patches);
-		strvec_clear(&am.args);
+		child_process_clear(&am);
 		return status;
 	}
 
diff --git a/t/t3438-rebase-broken-files.sh b/t/t3438-rebase-broken-files.sh
index c614c4f2e4..821f08e5af 100755
--- a/t/t3438-rebase-broken-files.sh
+++ b/t/t3438-rebase-broken-files.sh
@@ -58,4 +58,13 @@ test_expect_success 'unknown key in author-script' '
 	check_resolve_fails
 '
 
+test_expect_success POSIXPERM,SANITY 'unwritable rebased-patches does not leak' '
+	>.git/rebased-patches &&
+	chmod a-w .git/rebased-patches &&
+
+	git checkout -b side HEAD^ &&
+	test_commit unrelated &&
+	test_must_fail git rebase --apply --onto tmp HEAD^
+'
+
 test_done
-- 
2.44.0.682.g01e1dab148

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rebase: use child_process_clear() to clean
  2024-03-22 10:35 [PATCH] rebase: use child_process_clear() to clean Jeff King
@ 2024-03-22 17:21 ` Junio C Hamano
  2024-03-22 23:36   ` Jeff King
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2024-03-22 17:21 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

> In the run_am() function, we set up a child_process struct to run
> "git-am", allocating memory for its args and env strvecs. These are
> normally cleaned up when we call run_command(). But if we encounter
> certain errors, we exit the function early and try to clean up ourselves
> by clearing the am.args field. This leaks the "env" strvec.
>
> We should use child_process_clear() instead, which covers both. And more
> importantly, it future proofs us against the struct ever growing more
> allocated fields.

When 21853626 (built-in rebase: call `git am` directly, 2019-01-18)
started using run_command() API to drive "am", there already was
child_process API and child_process_clear() did clear both .args and
.env_array members but we used argv_array_clear() only to clear
am.args, leaking am.env_array.

> These are unlikely errors to happen in practice, so they don't actually
> trigger the leak sanitizer in the tests. But we can add a new test which
> does exercise one of the paths (and fails SANITIZE=leak without this
> patch).
> ...
> Not sure if the test is overkill. It really does nothing useful in a
> non-leak-checking build.

I wonder what are in .env array at this point, though, but that is
mere curiosity and not a problem with this patch.

Thanks.  Will queue.


> diff --git a/t/t3438-rebase-broken-files.sh b/t/t3438-rebase-broken-files.sh
> index c614c4f2e4..821f08e5af 100755
> --- a/t/t3438-rebase-broken-files.sh
> +++ b/t/t3438-rebase-broken-files.sh
> @@ -58,4 +58,13 @@ test_expect_success 'unknown key in author-script' '
>  	check_resolve_fails
>  '
>  
> +test_expect_success POSIXPERM,SANITY 'unwritable rebased-patches does not leak' '
> +	>.git/rebased-patches &&
> +	chmod a-w .git/rebased-patches &&
> +
> +	git checkout -b side HEAD^ &&
> +	test_commit unrelated &&
> +	test_must_fail git rebase --apply --onto tmp HEAD^
> +'
> +
>  test_done

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rebase: use child_process_clear() to clean
  2024-03-22 17:21 ` Junio C Hamano
@ 2024-03-22 23:36   ` Jeff King
  2024-03-25 18:44     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff King @ 2024-03-22 23:36 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Fri, Mar 22, 2024 at 10:21:23AM -0700, Junio C Hamano wrote:

> > We should use child_process_clear() instead, which covers both. And more
> > importantly, it future proofs us against the struct ever growing more
> > allocated fields.
> 
> When 21853626 (built-in rebase: call `git am` directly, 2019-01-18)
> started using run_command() API to drive "am", there already was
> child_process API and child_process_clear() did clear both .args and
> .env_array members but we used argv_array_clear() only to clear
> am.args, leaking am.env_array.

Ah, interesting. I didn't bother to dig into the history. Back then,
though, I think that "args" was the only allocated thing. The env call
was added much later in be0d29d301 (rebase --apply: make reflog messages
match rebase --merge, 2022-10-12).

> I wonder what are in .env array at this point, though, but that is
> mere curiosity and not a problem with this patch.

It's $GIT_REFLOG_ACTION for the sub-process.

-Peff

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rebase: use child_process_clear() to clean
  2024-03-22 23:36   ` Jeff King
@ 2024-03-25 18:44     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2024-03-25 18:44 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

>> I wonder what are in .env array at this point, though, but that is
>> mere curiosity and not a problem with this patch.
>
> It's $GIT_REFLOG_ACTION for the sub-process.

Ah, OK.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-25 18:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-22 10:35 [PATCH] rebase: use child_process_clear() to clean Jeff King
2024-03-22 17:21 ` Junio C Hamano
2024-03-22 23:36   ` Jeff King
2024-03-25 18:44     ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.