diff options
-rw-r--r-- | miscutils/make.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/miscutils/make.c b/miscutils/make.c index 8394c4e7b..bcba080fe 100644 --- a/miscutils/make.c +++ b/miscutils/make.c | |||
@@ -1627,23 +1627,24 @@ make_fgets(char *s, int size, FILE *fd) | |||
1627 | static char * | 1627 | static char * |
1628 | readline(FILE *fd) | 1628 | readline(FILE *fd) |
1629 | { | 1629 | { |
1630 | char *p, *str; | 1630 | char *p, *str = NULL; |
1631 | int pos = 0; | 1631 | int pos = 0; |
1632 | int len = 256; | 1632 | int len = 0; |
1633 | |||
1634 | str = xmalloc(len); | ||
1635 | 1633 | ||
1636 | for (;;) { | 1634 | for (;;) { |
1637 | if (make_fgets(str + pos, len - pos, fd) == NULL) { | 1635 | // We need room for at least one character and a NUL terminator |
1636 | if (len - pos > 1 && | ||
1637 | make_fgets(str + pos, len - pos, fd) == NULL) { | ||
1638 | if (pos) | 1638 | if (pos) |
1639 | return str; | 1639 | return str; |
1640 | free(str); | 1640 | free(str); |
1641 | return NULL; // EOF | 1641 | return NULL; // EOF |
1642 | } | 1642 | } |
1643 | 1643 | ||
1644 | if ((p = strchr(str + pos, '\n')) == NULL) { | 1644 | if (len - pos < 2 || (p = strchr(str + pos, '\n')) == NULL) { |
1645 | // Need more room | 1645 | // Need more room |
1646 | pos = len - 1; | 1646 | if (len) |
1647 | pos = len - 1; | ||
1647 | len += 256; | 1648 | len += 256; |
1648 | str = xrealloc(str, len); | 1649 | str = xrealloc(str, len); |
1649 | continue; | 1650 | continue; |