fix select usage for high fd's (dynamically allocate readfds)

This commit is contained in:
Todd C. Miller
1998-01-23 03:53:52 +00:00
parent 7baad7a52f
commit 0e8cf35a7b

View File

@@ -114,7 +114,7 @@ char * tgetpass(prompt, timeout, user, host)
int n, echo; int n, echo;
FILE *input, *output; FILE *input, *output;
static char buf[_PASSWD_LEN + 1]; static char buf[_PASSWD_LEN + 1];
fd_set readfds; fd_set *readfds;
struct timeval tv; struct timeval tv;
char *p; char *p;
@@ -199,8 +199,14 @@ char * tgetpass(prompt, timeout, user, host)
*/ */
if (timeout > 0) { if (timeout > 0) {
/* setup for select(2) */ /* setup for select(2) */
FD_ZERO(&readfds); n = howmany(fileno(input) + 1, NFDBITS) * sizeof(fd_mask);
FD_SET(fileno(input), &readfds); if ((readfds = (fd_set *) malloc(n)) == NULL) {
(void) fprintf(stderr, "Cannot allocate memory: ");
perror("");
return(NULL);
}
(void) memset((VOID *)readfds, 0, n);
FD_SET(fileno(input), readfds);
/* set timeout for select */ /* set timeout for select */
tv.tv_sec = timeout; tv.tv_sec = timeout;
@@ -210,12 +216,13 @@ char * tgetpass(prompt, timeout, user, host)
* get password or return empty string if nothing to read by timeout * get password or return empty string if nothing to read by timeout
*/ */
buf[0] = '\0'; buf[0] = '\0';
if (select(fileno(input) + 1, &readfds, 0, 0, &tv) > 0 && if (select(fileno(input) + 1, readfds, 0, 0, &tv) > 0 &&
fgets(buf, sizeof(buf), input)) { fgets(buf, sizeof(buf), input)) {
n = strlen(buf); n = strlen(buf);
if (buf[n - 1] == '\n') if (buf[n - 1] == '\n')
buf[n - 1] = '\0'; buf[n - 1] = '\0';
} }
(void) free(readfds);
} else { } else {
buf[0] = '\0'; buf[0] = '\0';
if (fgets(buf, sizeof(buf), input)) { if (fgets(buf, sizeof(buf), input)) {