Volume XXII, number 279Tuesday, October 6, 2026Latest message 38 minutes ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

patchstrbuf: avoid redundant reset in strbuf_getwholeline()

3 messages between Jul 14, 2026 and Jul 14, 2026, from René Scharfe, Junio C Hamano, Jeff King.

Plain Markdown or JSON for tools and agents. Diffs are folded; open one to read it.

René ScharfeJul 14, 2026, 08:45 UTC on lore

The HAVE_GETDELIM variant of strbuf_getwholeline() calls strbuf_reset() on the strbuf before handing it over to getdelim(3). This is unnecessary:

  - getdelim(3) doesn't care whether the old buffer contents is
    NUL-terminated and has no access to ->len,
  - on success getdelim(3) NUL-terminates the buffer and we set ->len,
  - on error we either call strbuf_init() or strbuf_reset().
Remove the superfluous preparatory call.
Signed-off-by: René Scharfe <l.s.r@web.de>
---
 strbuf.c | 2 --
 1 file changed, 2 deletions(-)
Show changes to strbuf.c +0 −2
diff --git a/strbuf.c b/strbuf.c
index 764b629927..44955669e8 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -646,8 +646,6 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 	if (feof(fp))
 		return EOF;
 
-	strbuf_reset(sb);
-
 	/* Translate slopbuf to NULL, as we cannot call realloc on it */
 	if (!sb->alloc)
 		sb->buf = NULL;
-- 
2.55.0
Junio C HamanoJul 14, 2026, 16:40 UTC in reply to René Scharfe on lore

Re: [PATCH] strbuf: avoid redundant reset in strbuf_getwholeline()

René Scharfe <l.s.r@web.de> writes:
Show 26 quoted lines
> The HAVE_GETDELIM variant of strbuf_getwholeline() calls strbuf_reset()
> on the strbuf before handing it over to getdelim(3).  This is
> unnecessary:
>
>   - getdelim(3) doesn't care whether the old buffer contents is
>     NUL-terminated and has no access to ->len,
>   - on success getdelim(3) NUL-terminates the buffer and we set ->len,
>   - on error we either call strbuf_init() or strbuf_reset().
>
> Remove the superfluous preparatory call.
>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  strbuf.c | 2 --
>  1 file changed, 2 deletions(-)
>
> diff --git a/strbuf.c b/strbuf.c
> index 764b629927..44955669e8 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -646,8 +646,6 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
>  	if (feof(fp))
>  		return EOF;
>  
> -	strbuf_reset(sb);
> -
This is well explained and makes perfect sense.
Thanks.  Will apply and mark for 'next'.
>  	/* Translate slopbuf to NULL, as we cannot call realloc on it */
>  	if (!sb->alloc)
>  		sb->buf = NULL;
Jeff KingJul 14, 2026, 21:49 UTC in reply to René Scharfe on lore

Re: [PATCH] strbuf: avoid redundant reset in strbuf_getwholeline()

On Tue, Jul 14, 2026 at 10:45:59AM +0200, René Scharfe wrote:
Show 10 quoted lines
> The HAVE_GETDELIM variant of strbuf_getwholeline() calls strbuf_reset()
> on the strbuf before handing it over to getdelim(3).  This is
> unnecessary:
> 
>   - getdelim(3) doesn't care whether the old buffer contents is
>     NUL-terminated and has no access to ->len,
>   - on success getdelim(3) NUL-terminates the buffer and we set ->len,
>   - on error we either call strbuf_init() or strbuf_reset().
> 
> Remove the superfluous preparatory call.

Good catch. In the original version of strbuf_getwholeline() we were missing that reset on error, which is why this was included. I think it became redundant in b70904306f (strbuf_getwholeline: NUL-terminate getdelim buffer on error, 2016-03-05).

-Peff

Back to recent threads