# [PATCH] grep: use return value of strbuf_detach()

2 messages from 2019-08-25 to 2019-08-26. Participants: René Scharfe, Johannes Schindelin.
Thread: https://gitlist.dev/t/51729

## René Scharfe, 2019-08-25 13:26

Subject: [PATCH] grep: use return value of strbuf_detach()
Message-ID: <caf49742-65ee-7b2a-0179-ee11822b2350@web.de>
URL: https://gitlist.dev/e/caf49742-65ee-7b2a-0179-ee11822b2350%40web.de

```
Append the strbuf buffer only after detaching it.  There is no practical
difference here, as the strbuf is not empty and no strbuf_ function is
called between storing the pointer to the still attached buffer and
calling strbuf_detach(), so that pointer is valid, but make sure to
follow the standard sequence anyway for consistency.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 builtin/grep.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 2699001fbd..69ac053acc 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -1110,8 +1110,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			strbuf_addf(&buf, "+/%s%s",
 					strcmp("less", pager) ? "" : "*",
 					opt.pattern_list->pattern);
-			string_list_append(&path_list, buf.buf);
-			strbuf_detach(&buf, NULL);
+			string_list_append(&path_list,
+					   strbuf_detach(&buf, NULL));
 		}
 	}

--
2.23.0

```

## Johannes Schindelin, 2019-08-26 14:58

Subject: Re: [PATCH] grep: use return value of strbuf_detach()
Message-ID: <nycvar.QRO.7.76.6.1908261658210.46@tvgsbejvaqbjf.bet>
URL: https://gitlist.dev/e/nycvar.QRO.7.76.6.1908261658210.46%40tvgsbejvaqbjf.bet
In-Reply-To: <caf49742-65ee-7b2a-0179-ee11822b2350@web.de>

```
Hi René,

On Sun, 25 Aug 2019, René Scharfe wrote:

> Append the strbuf buffer only after detaching it.  There is no practical
> difference here, as the strbuf is not empty and no strbuf_ function is
> called between storing the pointer to the still attached buffer and
> calling strbuf_detach(), so that pointer is valid, but make sure to
> follow the standard sequence anyway for consistency.

ACK!

Thanks,
Dscho

>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  builtin/grep.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/grep.c b/builtin/grep.c
> index 2699001fbd..69ac053acc 100644
> --- a/builtin/grep.c
> +++ b/builtin/grep.c
> @@ -1110,8 +1110,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>  			strbuf_addf(&buf, "+/%s%s",
>  					strcmp("less", pager) ? "" : "*",
>  					opt.pattern_list->pattern);
> -			string_list_append(&path_list, buf.buf);
> -			strbuf_detach(&buf, NULL);
> +			string_list_append(&path_list,
> +					   strbuf_detach(&buf, NULL));
>  		}
>  	}
>
> --
> 2.23.0
>

```
