From eef9f6d258acd453e4d709c0ef38a5c336f1744d Mon Sep 17 00:00:00 2001 From: Marcel Moolenaar Date: Sat, 5 Apr 2014 18:14:58 +0000 Subject: [PATCH] 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 --- lib/libc/gen/getlogin.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/libc/gen/getlogin.c b/lib/libc/gen/getlogin.c index 4dab3cb07f2..7ab4cba88a4 100644 --- a/lib/libc/gen/getlogin.c +++ b/lib/libc/gen/getlogin.c @@ -87,11 +87,16 @@ getlogin_r(char *logname, int namelen) char *result; int len; int status; - + + if (namelen < 1) + return (ERANGE); + logname[0] = '\0'; + THREAD_LOCK(); result = getlogin_basic(&status); - if (status == 0) { - if ((len = strlen(result) + 1) > namelen) + if (status == 0 && result != NULL) { + len = strlen(result) + 1; + if (len > namelen) status = ERANGE; else strncpy(logname, result, len);