From a3f5a043fa5b3cb9ae1c684708399f26716ae451 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 21 Aug 2024 14:41:13 -0700 Subject: [PATCH] Handle getlogin failure in PTY.spawn getlogin is only called if USER environment variable is not set, but if getlogin returns NULL in that case, then do not call getpwnam, and assume /bin/sh as shell. Mentioned in comment to bug 20586. --- ext/pty/pty.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/pty/pty.c b/ext/pty/pty.c index 5ca55e41d6..49caa0b79f 100644 --- a/ext/pty/pty.c +++ b/ext/pty/pty.c @@ -215,9 +215,13 @@ establishShell(int argc, VALUE *argv, struct pty_info *info, else { #if defined HAVE_PWD_H const char *username = getenv("USER"); - struct passwd *pwent = getpwnam(username ? username : getlogin()); - if (pwent && pwent->pw_shell) - shellname = pwent->pw_shell; + if (username == NULL) + username = getlogin(); + if (username != NULL) { + struct passwd *pwent = getpwnam(username); + if (pwent && pwent->pw_shell) + shellname = pwent->pw_shell; + } #endif } v = rb_str_new2(shellname);