comparison urogue/mdport.c @ 270:7a96fede6cc8

UltraRogue: check the return value from getpwuid(). It is possible for getpwuid() to return NULL. Such a failure will no longer cause a segfault. However, the call to getpwuid() may normally not be reachable.
author John "Elwin" Edwards
date Wed, 01 Mar 2017 20:40:18 -0500
parents c495a4f288c6
children 1db299e868b8
comparison
equal deleted inserted replaced
269:a413bc97d3ea 270:7a96fede6cc8
242 #if !defined(_WIN32) && !defined(DJGPP) 242 #if !defined(_WIN32) && !defined(DJGPP)
243 struct passwd *pw; 243 struct passwd *pw;
244 244
245 pw = getpwuid(getuid()); 245 pw = getpwuid(getuid());
246 246
247 l = pw->pw_name; 247 if (pw != NULL)
248 l = pw->pw_name;
248 #endif 249 #endif
249 250
250 if ((l == NULL) || (*l == '\0')) 251 if ((l == NULL) || (*l == '\0'))
251 if ( (l = getenv("USERNAME")) == NULL ) 252 if ( (l = getenv("USERNAME")) == NULL )
252 if ( (l = getenv("LOGNAME")) == NULL ) 253 if ( (l = getenv("LOGNAME")) == NULL )
273 #else 274 #else
274 char slash = '/'; 275 char slash = '/';
275 struct passwd *pw; 276 struct passwd *pw;
276 pw = getpwuid(getuid()); 277 pw = getpwuid(getuid());
277 278
278 h = pw->pw_dir; 279 if (pw != NULL)
280 h = pw->pw_dir;
279 281
280 if (strcmp(h,"/") == 0) 282 if (strcmp(h,"/") == 0)
281 h = NULL; 283 h = NULL;
282 #endif 284 #endif
283 homedir[0] = 0; 285 homedir[0] = 0;
333 char *def = "C:\\COMMAND.COM"; 335 char *def = "C:\\COMMAND.COM";
334 #else 336 #else
335 char *def = "/bin/sh"; 337 char *def = "/bin/sh";
336 struct passwd *pw; 338 struct passwd *pw;
337 pw = getpwuid(getuid()); 339 pw = getpwuid(getuid());
338 s = pw->pw_shell; 340 if (pw != NULL)
341 s = pw->pw_shell;
339 #endif 342 #endif
340 if ((s == NULL) || (*s == '\0')) 343 if ((s == NULL) || (*s == '\0'))
341 if ( (s = getenv("COMSPEC")) == NULL) 344 if ( (s = getenv("COMSPEC")) == NULL)
342 if ( (s = getenv("SHELL")) == NULL) 345 if ( (s = getenv("SHELL")) == NULL)
343 if ( (s = getenv("SystemRoot")) == NULL) 346 if ( (s = getenv("SystemRoot")) == NULL)
453 { 456 {
454 static char uidstr[20]; 457 static char uidstr[20];
455 #if !defined(_WIN32) && !defined(DJGPP) 458 #if !defined(_WIN32) && !defined(DJGPP)
456 struct passwd *pp; 459 struct passwd *pp;
457 460
458 if ((pp = getpwuid(uid)) == NULL) 461 if ((pp = getpwuid(uid)) == NULL)
459 { 462 {
460 sprintf(uidstr,"%d", uid); 463 sprintf(uidstr,"%d", uid);
461 return(uidstr); 464 return(uidstr);
462 } 465 }
463 else 466 else
464 return(pp->pw_name); 467 return(pp->pw_name);
465 #else 468 #else
466 sprintf(uidstr,"%d", uid); 469 sprintf(uidstr,"%d", uid);
467 return(uidstr); 470 return(uidstr);
468 #endif 471 #endif
469 } 472 }