]> granicus.if.org Git - sudo/commitdiff
When read()'ing, do a single character at a time to be sure we don't go
authorTodd C. Miller <Todd.Miller@courtesan.com>
Mon, 1 Nov 1999 15:58:46 +0000 (15:58 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Mon, 1 Nov 1999 15:58:46 +0000 (15:58 +0000)
oast the newline.

tgetpass.c

index 213c32ac2fd9f99ba30bea2f3a849f086cd2f324..df079888ca3abc44cf80070a92b242605833f0f8 100644 (file)
@@ -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);
 }