From 2ff3dc061997148d5f890f754bfaa9db93a6ee11 Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Fri, 16 Oct 2009 14:30:06 +0000 Subject: [PATCH] Fix segfault on getpwuid failure --- rogue3/mdport.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/rogue3/mdport.c b/rogue3/mdport.c index 7b4477c..03702dd 100644 --- a/rogue3/mdport.c +++ b/rogue3/mdport.c @@ -444,8 +444,11 @@ md_getusername(void) struct passwd *pw; pw = getpwuid(getuid()); - - l = pw->pw_name; + /* Don't segfault if getpwuid fails (and the thing is wildly possible) */ + if (pw != NULL) + l = pw->pw_name; + else + l = NULL; #endif if ((l == NULL) || (*l == '\0')) @@ -475,8 +478,11 @@ md_gethomedir(void) char slash = '/'; struct passwd *pw; pw = getpwuid(getuid()); - - h = pw->pw_dir; + /* Don't segfault if getpwuid fails */ + if (pw != NULL) + h = pw->pw_dir; + else + h = NULL; if (strcmp(h,"/") == 0) h = NULL; @@ -540,7 +546,11 @@ md_getshell(void) char *def = "/bin/sh"; struct passwd *pw; pw = getpwuid(getuid()); - s = pw->pw_shell; + /* don't segfault if getpwuid fails */ + if (pw != NULL) + s = pw->pw_shell; + else + s = NULL; #endif if ((s == NULL) || (*s == '\0')) if ( (s = getenv("COMSPEC")) == NULL)