BUG #4837: initdb segv's if getpwuid fails - Mailing list pgsql-bugs

From David Vitek
Subject BUG #4837: initdb segv's if getpwuid fails
Date
Msg-id 200906031521.n53FL85e054804@wwwmaster.postgresql.org
Whole thread Raw
Responses Re: BUG #4837: initdb segv's if getpwuid fails
List pgsql-bugs
The following bug has been logged online:

Bug reference:      4837
Logged by:          David Vitek
Email address:      dvitek@grammatech.com
PostgreSQL version: 8.3
Operating system:   Non-Windows
Description:        initdb segv's if getpwuid fails
Details:

We've seen this happen in a deployment, and we are currently assuming the
user has some configuration problem.  Here's a patch (ignore the
revisions):

Index: src/bin/initdb/initdb.c
===================================================================
--- src/bin/initdb/initdb.c     (revision 48282)
+++ src/bin/initdb/initdb.c     (working copy)
@@ -660,10 +660,18 @@
 #ifndef WIN32

        struct passwd *pw;
+        uid_t uid = geteuid();

-       pw = getpwuid(geteuid());
+        errno = 0; /* getpwuid may not alter errno on failure */
+       pw = getpwuid(uid);
+        if( !pw )
+        {
+            fprintf(stderr, _("%s: getpwuid failed: %s\n"),
+                    progname, strerror(errno));
+            exit(1);
+        }

-       if (geteuid() == 0)                     /* 0 is root's uid */
+       if (uid == 0)                   /* 0 is root's uid */
        {
                fprintf(stderr,
                                _("%s: cannot be run as root\n"

pgsql-bugs by date:

Previous
From: Anurag Pathak
Date:
Subject: Unable to find UUID Option while Installing PostGreSQL 8.3
Next
From: Tom Lane
Date:
Subject: Re: BUG #4835: psql server crashes when using non-existing functions