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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi

On Mon, 26 Feb 2007, Junio C Hamano wrote:

> [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@xxxxxxx>
> ---
> 
>  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

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]