From: Tom Yu Date: Fri, 5 Dec 1997 02:48:58 +0000 (+0000) Subject: * getpty.c (pty_getpty): Fix checks on string lengths to account X-Git-Tag: krb5-1.1-beta1~929 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1c19e9f07aacf52c9775e0613cccca650b3268df;p=krb5.git * getpty.c (pty_getpty): Fix checks on string lengths to account for terminating nul character. Some whitespace fixups. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@10313 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/util/pty/ChangeLog b/src/util/pty/ChangeLog index b25dc16e7..ebe846adc 100644 --- a/src/util/pty/ChangeLog +++ b/src/util/pty/ChangeLog @@ -1,3 +1,8 @@ +Thu Dec 4 21:48:12 1997 Tom Yu + + * getpty.c (pty_getpty): Fix checks on string lengths to account + for terminating nul character. Some whitespace fixups. + Wed Dec 3 17:16:44 1997 Tom Yu * pty_err.et: Add PTY_OPEN_SLAVE_TOOSHORT error code. diff --git a/src/util/pty/getpty.c b/src/util/pty/getpty.c index 8b97bff00..4fe6459a6 100644 --- a/src/util/pty/getpty.c +++ b/src/util/pty/getpty.c @@ -25,13 +25,13 @@ long pty_getpty (fd, slave, slavelength) int slavelength; int *fd; char *slave; { - char *cp; + char *cp; char *p; int i,ptynum; struct stat stb; -char slavebuf[1024]; + char slavebuf[1024]; #ifdef HAVE__GETPTY -char *slaveret; /*Temporary to hold pointer to slave*/ + char *slaveret; /*Temporary to hold pointer to slave*/ #endif /*HAVE__GETPTY*/ #ifdef HAVE_OPENPTY @@ -39,7 +39,7 @@ char *slaveret; /*Temporary to hold pointer to slave*/ if(openpty(fd, &slavefd, slave, (struct termios *) 0, (struct winsize *) 0)) return 1; -close(slavefd); + close(slavefd); return 0; #else /*HAVE_OPENPTY*/ #ifdef HAVE__GETPTY @@ -51,13 +51,13 @@ close(slavefd); * paths. */ if ((slaveret = _getpty(fd, O_RDWR|O_NDELAY, 0600, 0)) == 0) { - *fd = -1; - return PTY_GETPTY_NOPTY; + *fd = -1; + return PTY_GETPTY_NOPTY; } - if (strlen(slaveret) > slavelength) { - close(*fd); - *fd = -1; - return PTY_GETPTY_SLAVE_TOOLONG; + if (strlen(slaveret) > slavelength - 1) { + close(*fd); + *fd = -1; + return PTY_GETPTY_SLAVE_TOOLONG; } else strcpy(slave, slaveret); return 0; @@ -86,13 +86,11 @@ close(slavefd); #endif #endif if (p) { - if ( strlen(p) > slavelength) - { + if (strlen(p) > slavelength - 1) { close (*fd); *fd = -1; return PTY_GETPTY_SLAVE_TOOLONG; - } - + } strcpy(slave, p); return 0; } @@ -102,21 +100,19 @@ close(slavefd); return PTY_GETPTY_FSTAT; } ptynum = (int)(stb.st_rdev&0xFF); - sprintf(slavebuf, "/dev/ttyp%x", ptynum); - if ( strlen(slavebuf) > slavelength) { - close(*fd); - *fd = -1; - return PTY_GETPTY_SLAVE_TOOLONG; - } - strncpy ( slave, slavebuf, slavelength); + sprintf(slavebuf, "/dev/ttyp%x", ptynum); + if (strlen(slavebuf) > slavelength - 1) { + close(*fd); + *fd = -1; + return PTY_GETPTY_SLAVE_TOOLONG; + } + strncpy(slave, slavebuf, slavelength); return 0; - } else { - - for (cp = "pqrstuvwxyzPQRST";*cp; cp++) { + for (cp = "pqrstuvwxyzPQRST";*cp; cp++) { sprintf(slavebuf,"/dev/ptyXX"); - slavebuf[strlen("/dev/pty")] = *cp; - slavebuf[strlen("/dev/ptyp")] = '0'; + slavebuf[sizeof("/dev/pty")] = *cp; + slavebuf[sizeof("/dev/ptyp")] = '0'; if (stat(slavebuf, &stb) < 0) break; for (i = 0; i < 16; i++) { @@ -126,13 +122,12 @@ close(slavefd); /* got pty */ slavebuf[strlen("/dev/")] = 't'; - if ( strlen(slavebuf) > slavelength ) { - close ( *fd); + if (strlen(slavebuf) > slavelength -1) { + close(*fd); *fd = -1; -return PTY_GETPTY_SLAVE_TOOLONG; + return PTY_GETPTY_SLAVE_TOOLONG; } - strncpy ( slave, slavebuf, slavelength); - + strncpy(slave, slavebuf, slavelength); return 0; } } @@ -141,4 +136,3 @@ return PTY_GETPTY_SLAVE_TOOLONG; #endif /*HAVE__GETPTY*/ #endif /* HAVE_OPENPTY */ } -