-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix username lookup in case of missing USER environment variable #245
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,12 @@ | |
#ifdef HAVE_SYSLOG_H | ||
# include <syslog.h> | ||
#endif | ||
#ifdef HAVE_UNISTD_H | ||
# include <unistd.h> // For geteuid. | ||
#endif | ||
#ifdef HAVE_PWD_H | ||
# include <pwd.h> | ||
#endif | ||
|
||
#include "base/googleinit.h" | ||
|
||
|
@@ -299,8 +305,26 @@ static void MyUserNameInitializer() { | |
if (user != NULL) { | ||
g_my_user_name = user; | ||
} else { | ||
g_my_user_name = "invalid-user"; | ||
#if defined(HAVE_PWD_H) && defined(HAVE_UNISTD_H) | ||
uid_t uid; | ||
struct passwd pwd; | ||
struct passwd*result = NULL; | ||
char buffer[1024] = {'\0'}; | ||
uid = geteuid(); | ||
int pwuid_res = getpwuid_r(uid, &pwd, buffer, sizeof (buffer), &result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you remove the whitespace between sizeof and buffer? |
||
if(pwuid_res == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A whitespace after "if" |
||
g_my_user_name = pwd.pw_name; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please concatenate with the next line |
||
else { | ||
snprintf(buffer, sizeof(buffer), "uid%d", uid); | ||
g_my_user_name = buffer; | ||
} | ||
#endif | ||
if(g_my_user_name.empty()) { | ||
g_my_user_name = "invalid-user"; | ||
} | ||
} | ||
|
||
} | ||
REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer()); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a whitespace right before "result"?
struct passwd* result = NULL