From bc94410ecee25ec26d6e0d22213af159154b1a64 Mon Sep 17 00:00:00 2001 From: Cam Mannett Date: Tue, 25 Jun 2024 09:21:47 +0100 Subject: [PATCH] Provide fallbacks within POSIX GetHomeFolderFilename() There are scenarios (specifically when running in anonymous containers) where there is no entry in the password file for the current user, this causes getpwuid() to return a NULL pointer promptly causing a segfault. This change detects this and then attempts to read the $HOME env var, and if that fails it falls back to the temp dir. Although the temp dir is not valid as a home dir, it is a valid path and so can be used by the calling function. --- renderdoc/os/posix/posix_stringio.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/renderdoc/os/posix/posix_stringio.cpp b/renderdoc/os/posix/posix_stringio.cpp index 896248cf5..5d0778727 100644 --- a/renderdoc/os/posix/posix_stringio.cpp +++ b/renderdoc/os/posix/posix_stringio.cpp @@ -53,10 +53,23 @@ rdcstr GetTempRootPath(); rdcstr GetHomeFolderFilename() { - passwd *pw = getpwuid(getuid()); - const char *homedir = pw->pw_dir; + errno = 0; + const uid_t uid = getuid(); + const passwd *pw = getpwuid(uid); + if(pw != NULL) + { + return pw->pw_dir; + } - return homedir; + RDCERR("Cannot find password file entry for %u: %s, falling back to $HOME", uid, strerror(errno)); + const rdcstr homeEnv = Process::GetEnvVariable("HOME"); + if(!homeEnv.empty()) + { + return homeEnv; + } + + RDCERR("$HOME is empty, returning temp path"); + return GetTempFolderFilename(); } rdcstr GetTempFolderFilename()