Skip to content

Commit

Permalink
Provide fallbacks within POSIX GetHomeFolderFilename()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmannett85-arm authored and baldurk committed Jun 25, 2024
1 parent 4648ef2 commit bc94410
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions renderdoc/os/posix/posix_stringio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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;
}

return homedir;
RDCERR("$HOME is empty, returning temp path");
return GetTempFolderFilename();
}

rdcstr GetTempFolderFilename()
Expand Down

0 comments on commit bc94410

Please sign in to comment.