# [PATCH] am: release strbuf after use in split_mail_mbox()

2 messages from 2017-12-07 to 2017-12-07. Participants: René Scharfe, Jeff King.
Thread: https://gitlist.dev/t/47399

## René Scharfe, 2017-12-07 20:20

Subject: [PATCH] am: release strbuf after use in split_mail_mbox()
Message-ID: <ad0af192-1bd4-eee2-d6e8-2f1776e87428@web.de>
URL: https://gitlist.dev/e/ad0af192-1bd4-eee2-d6e8-2f1776e87428%40web.de

```
Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/am.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 02853b3e05..1ac044da2e 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -708,6 +708,7 @@ static int split_mail_mbox(struct am_state *state, const char **paths,
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
 	struct strbuf last = STRBUF_INIT;
+	int ret;
 
 	cp.git_cmd = 1;
 	argv_array_push(&cp.args, "mailsplit");
@@ -721,13 +722,16 @@ static int split_mail_mbox(struct am_state *state, const char **paths,
 	argv_array_push(&cp.args, "--");
 	argv_array_pushv(&cp.args, paths);
 
-	if (capture_command(&cp, &last, 8))
-		return -1;
+	ret = capture_command(&cp, &last, 8);
+	if (ret)
+		goto exit;
 
 	state->cur = 1;
 	state->last = strtol(last.buf, NULL, 10);
 
-	return 0;
+exit:
+	strbuf_release(&last);
+	return ret ? -1 : 0;
 }
 
 /**
-- 
2.15.1

```

## Jeff King, 2017-12-07 21:20

Subject: Re: [PATCH] am: release strbuf after use in split_mail_mbox()
Message-ID: <20171207212030.GC12850@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20171207212030.GC12850%40sigill.intra.peff.net
In-Reply-To: <ad0af192-1bd4-eee2-d6e8-2f1776e87428@web.de>

```
On Thu, Dec 07, 2017 at 09:20:19PM +0100, René Scharfe wrote:

> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  builtin/am.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/builtin/am.c b/builtin/am.c
> index 02853b3e05..1ac044da2e 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -708,6 +708,7 @@ static int split_mail_mbox(struct am_state *state, const char **paths,
>  {
>  	struct child_process cp = CHILD_PROCESS_INIT;
>  	struct strbuf last = STRBUF_INIT;
> +	int ret;
>  
>  	cp.git_cmd = 1;
>  	argv_array_push(&cp.args, "mailsplit");
> @@ -721,13 +722,16 @@ static int split_mail_mbox(struct am_state *state, const char **paths,
>  	argv_array_push(&cp.args, "--");
>  	argv_array_pushv(&cp.args, paths);
>  
> -	if (capture_command(&cp, &last, 8))
> -		return -1;
> +	ret = capture_command(&cp, &last, 8);
> +	if (ret)
> +		goto exit;

Looks good to me.

Coupled with your third patch, it made me wonder if capture_command()
should free the strbuf when it sees an error. But probably not. Some
callers would want to see the output even from a failing command (and
doubly for pipe_command(), which may capture stderr).

(And anyway, it wouldn't make this case any simpler; we were leaking in
the success code path, too!)

-Peff

```
