Fix an out-of-bounds write when a zero-length buffer is passed.

Found with ttyname_test and CHERI bounds checking.

Reviewed by:	emaste
Obtained from:	CheriBSD
MFC after:	1 week
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D10377
This commit is contained in:
Brooks Davis 2017-04-13 15:52:45 +00:00
parent 33c72b24de
commit 8439a7220d

View File

@ -61,6 +61,10 @@ ttyname_r(int fd, char *buf, size_t len)
{
size_t used;
/* Don't write off the end of a zero-length buffer. */
if (len < 1)
return (ERANGE);
*buf = '\0';
/* Must be a terminal. */