Fixed possible dereference of null pointer
authorMark Abraham <mark.j.abraham@gmail.com>
Thu, 11 Jul 2013 23:32:16 +0000 (00:32 +0100)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sun, 14 Jul 2013 03:28:00 +0000 (05:28 +0200)
getpwuid() can return null. For example, Shun Sakuruba reported that
on Cray XE6 compute nodes have passwd.h, but users will not have
passwd entries.

Fixes #1301

Change-Id: I66e8064438fc02591629b0d381bc00afd06795c0

src/gmxlib/string2.c

index 1b02ff798bed23dd0acb5f2685e83287b345880f..eadbf21a591804e985e429c36f6aae463e883821 100644 (file)
@@ -244,7 +244,8 @@ void nice_header (FILE *out, const char *fn)
     uid  = getuid();
     pw   = getpwuid(uid);
     gh   = gethostname(buf, 255);
-    user = pw->pw_name;
+    /* pw returns null on error (e.g. compute nodes lack /etc/passwd) */
+    user = pw ? pw->pw_name : unk;
 #else
     uid = 0;
     gh  = -1;