aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2024-06-02 19:36:03 +0100
committerRon Yorston <rmy@pobox.com>2024-06-02 19:36:03 +0100
commit75a18a60b40f87dd4d081bc75931c9f93de7b82c (patch)
treeafd08a2457858b2868741e2f3ef2917b985392ea
parentb845dac43ac170d8b5a40d0e75dbd4276e5a3df6 (diff)
downloadbusybox-w32-75a18a60b40f87dd4d081bc75931c9f93de7b82c.tar.gz
busybox-w32-75a18a60b40f87dd4d081bc75931c9f93de7b82c.tar.bz2
busybox-w32-75a18a60b40f87dd4d081bc75931c9f93de7b82c.zip
make: ensure sufficient space in line buffer
When using fgets(3) to read a line into a buffer it's necessary to ensure at least two characters are available in the buffer. Otherwise the read fails. (At least, it did when pdpmake was built using MSYS2 on Windows.) Adds 16 bytes to the 32-bit build. (GitHub pdpmake issue 44)
-rw-r--r--miscutils/make.c15
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)
1627static char * 1627static char *
1628readline(FILE *fd) 1628readline(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;