git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] strbuf_getwholeline: NUL-terminate getdelim buffer on error

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 5, 2016, 19:33 UTC
Message-ID
<xmqqziucu3d7.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160305184330.GA7534@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
> Not a big rush for 2.8.0-rc, as the bug is in v2.5.0, and I doubt
> there's an easy trigger besides fast-import. But it might be harmless
> enough to squeeze in.

Was it found by a real-world debugging session, or by a code inspection? I'd be really impressed if it were the latter ;-)

Thanks, will queue.
Show 40 quoted lines
>  strbuf.c               | 8 +++++++-
>  t/t9300-fast-import.sh | 4 ++++
>  2 files changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/strbuf.c b/strbuf.c
> index f60e2ee..2c08dbb 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -481,9 +481,15 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
>  	if (errno == ENOMEM)
>  		die("Out of memory, getdelim failed");
>  
> -	/* Restore slopbuf that we moved out of the way before */
> +	/*
> +	 * Restore strbuf invariants; if getdelim left us with a NULL pointer,
> +	 * we can just re-init, but otherwise we should make sure that our
> +	 * length is empty, and that the result is NUL-terminated.
> +	 */
>  	if (!sb->buf)
>  		strbuf_init(sb, 0);
> +	else
> +		strbuf_reset(sb);
>  	return EOF;
>  }
>  #else
> diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
> index 4c5f3c9..25bb60b 100755
> --- a/t/t9300-fast-import.sh
> +++ b/t/t9300-fast-import.sh
> @@ -55,6 +55,10 @@ test_expect_success 'empty stream succeeds' '
>  	git fast-import </dev/null
>  '
>  
> +test_expect_success 'truncated stream complains' '
> +	echo "tag foo" | test_must_fail git fast-import
> +'
> +
>  test_expect_success 'A: create pack from stdin' '
>  	test_tick &&
>  	cat >input <<-INPUT_END &&
Previous: Jeff KingNext: Jeff King
Message 2 of 3 in “strbuf_getwholeline: NUL-terminate getdelim buffer on error”
  1. strbuf_getwholeline: NUL-terminate getdelim buffer on errorJeff King, Mar 5, 2016
  2. Junio C HamanoMar 5, 2016
  3. Jeff KingMar 5, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.