From: Todd C. Miller Date: Mon, 1 Nov 1999 15:58:46 +0000 (+0000) Subject: When read()'ing, do a single character at a time to be sure we don't go X-Git-Tag: SUDO_1_6_0~22 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=68fd7e74c12f94080e54699ce0f16639f4f3a3ab;p=sudo When read()'ing, do a single character at a time to be sure we don't go oast the newline. --- diff --git a/tgetpass.c b/tgetpass.c index 213c32ac2..df079888c 100644 --- a/tgetpass.c +++ b/tgetpass.c @@ -182,8 +182,12 @@ tgetline(fd, buf, bufsiz, timeout) int n; fd_set *readfds = NULL; struct timeval tv; + char c; char *cp; + if (bufsiz == 0) + return(NULL); /* sanity */ + /* * Timeout of <= 0 means no timeout. */ @@ -210,16 +214,12 @@ tgetline(fd, buf, bufsiz, timeout) if (readfds) free(readfds); - /* Get a line of input */ - left = bufsiz; + /* Keep reading until out of space, EOF, error, or newline */ cp = buf; - do { - if ((n = read(fd, cp, left)) > 0) { - cp += n; - left -= n; - } - } while (n > 0 && left != 0 && *(cp - 1) != '\n'); - *(cp - 1) = '\0'; + left = bufsiz; + while (--left && (n = read(fd, &c, 1)) == 1 && c != '\n') + *cp++ = c; + *cp = '\0'; - return(left == bufsiz ? NULL : buf); + return(cp == buf ? NULL : buf); }