Avoid core dump after getpwuid_r failure.
authorTom Lane <[email protected]>
Sun, 8 Sep 2024 23:14:40 +0000 (19:14 -0400)
committerTom Lane <[email protected]>
Sun, 8 Sep 2024 23:14:40 +0000 (19:14 -0400)
Looking up a nonexistent user ID would lead to a null pointer
dereference.  That's unlikely to happen here, but perhaps
not impossible.

Thinko in commit 4d5111b3f, noticed by Coverity.

src/interfaces/libpq/fe-auth.c

index 4904d38ce1c2888510fdb2ad2e8fceaf9bf2ca35..20d3427e943adf623719ae58a831539d558f4f74 100644 (file)
@@ -1205,7 +1205,7 @@ pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
    DWORD       namesize = sizeof(username);
 #else
    struct passwd pwbuf;
-   struct passwd *pw;
+   struct passwd *pw = NULL;
    char        buf[1024];
    int         rc;
 #endif
@@ -1230,7 +1230,8 @@ pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
        if (errorMessage)
            libpq_append_error(errorMessage, "local user with ID %ld does not exist", (long) user_id);
    }
-   name = pw->pw_name;
+   else
+       name = pw->pw_name;
 #endif
 
    if (name)