@matheusbsbs / matheus-bagliano / commits / dec56c8cf1
Fork 0

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>

Junio C Hamano committed Feb 26, 2007 at 11:37 UTC dec56c8cf1da06fe9ff4c9d3a26f74fb1ddd2fc6
1 file changed +13 -6
builtin-fetch--tool.c
+13 -6
@@ -2,17 +2,24 @@
2 #include "refs.h"
3 #include "commit.h"
4
5 -#define CHUNK_SIZE (1048576)
5 +#define CHUNK_SIZE 1024
6
7 static char *get_stdin(void)
8 {
9 + int offset = 0;
10 char *data = xmalloc(CHUNK_SIZE);
10 - int offset = 0, read = 0;
11 - read = xread(0, data, CHUNK_SIZE);
12 - while (read == CHUNK_SIZE) {
13 - offset += CHUNK_SIZE;
11 +
12 + while (1) {
13 + int cnt = xread(0, data + offset, CHUNK_SIZE);
14 + if (cnt < 0)
15 + die("error reading standard input: %s",
16 + strerror(errno));
17 + if (cnt == 0) {
18 + data[offset] = 0;
19 + break;
20 + }
21 + offset += cnt;
22 data = xrealloc(data, offset + CHUNK_SIZE);
15 - read = xread(0, data + offset, CHUNK_SIZE);
23 }
24 return data;
25 }