The getlogin_basic() function can return a 0 status with a NULL

pointer for the login name (result). Make sure to handle that
case properly. Improve robustness by checking namelen and then
nul-terminating the provided buffer to simplify subsequent logic.

Obtained from:	Juniper Networks, Inc.
MFC after:	1 week
This commit is contained in:
marcel 2014-04-05 18:14:58 +00:00
parent 60abae8fbc
commit 444c6d5354

View File

@ -88,10 +88,15 @@ getlogin_r(char *logname, int namelen)
int len; int len;
int status; int status;
if (namelen < 1)
return (ERANGE);
logname[0] = '\0';
THREAD_LOCK(); THREAD_LOCK();
result = getlogin_basic(&status); result = getlogin_basic(&status);
if (status == 0) { if (status == 0 && result != NULL) {
if ((len = strlen(result) + 1) > namelen) len = strlen(result) + 1;
if (len > namelen)
status = ERANGE; status = ERANGE;
else else
strncpy(logname, result, len); strncpy(logname, result, len);