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

Re: [PATCH] fetch--tool: fix uninitialized buffer when reading from stdin

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Feb 27, 2007, 19:35 UTC
Message-ID
<Pine.LNX.4.63.0702272034010.22628@wbgn013.biozentrum.uni-wuerzburg.de>
In-Reply-To
<7vfy8sn20o.fsf@assigned-by-dhcp.cox.net>
Hi
On Mon, 26 Feb 2007, Junio C Hamano wrote:
Show 46 quoted lines
> [PATCH] fetch--tool: fix uninitialized buffer when reading from stdin
> 
> The original code allocates too much space and forgets to NUL
> terminate the string.
> 
> Signed-off-by: Junio C Hamano <junkio@cox.net>
> ---
> 
>  builtin-fetch--tool.c |   19 +++++++++++++------
>  1 files changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/builtin-fetch--tool.c b/builtin-fetch--tool.c
> index 48de08d..a068f8d 100644
> --- a/builtin-fetch--tool.c
> +++ b/builtin-fetch--tool.c
> @@ -2,17 +2,24 @@
>  #include "refs.h"
>  #include "commit.h"
>  
> -#define CHUNK_SIZE (1048576)
> +#define CHUNK_SIZE 1024
>  
>  static char *get_stdin(void)
>  {
> +	int offset = 0;
>  	char *data = xmalloc(CHUNK_SIZE);
> -	int offset = 0, read = 0;
> -	read = xread(0, data, CHUNK_SIZE);
> -	while (read == CHUNK_SIZE) {
> -		offset += CHUNK_SIZE;
> +
> +	while (1) {
> +		int cnt = xread(0, data + offset, CHUNK_SIZE);
> +		if (cnt < 0)
> +			die("error reading standard input: %s",
> +			    strerror(errno));
> +		if (cnt == 0) {
> +			data[offset] = 0;
> +			break;
> +		}
> +		offset += cnt;
>  		data = xrealloc(data, offset + CHUNK_SIZE);
> -		read = xread(0, data + offset, CHUNK_SIZE);
>  	}
>  	return data;
>  }
Me-liked-by: Dscho. And maybe people forget about my silly "fix"...

Ciao, Dscho

Previous: Junio C Hamano
Message 10 of 10 in “fetch--tool: fix uninitialized buffer when reading from stdin”
  1. fetch--tool: fix uninitialized buffer when reading from stdinJohannes Schindelin, Feb 26, 2007
  2. Linus TorvaldsFeb 26, 2007
  3. Johannes SchindelinFeb 26, 2007
  4. Linus TorvaldsFeb 26, 2007
  5. Johannes SchindelinFeb 26, 2007
  6. Johannes SchindelinFeb 26, 2007
  7. Linus TorvaldsFeb 26, 2007
  8. Junio C HamanoFeb 26, 2007
  9. Junio C HamanoFeb 26, 2007
  10. Johannes SchindelinFeb 27, 2007

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.