Fix UNIX crash on startup when HOME isn't set
Regression in [0] which re-introduced #2931. Accessing environment variables must always null check the result. Resolve by checking the result as well as replacing $HOME access with a function that falls back to `passwd::pw_dir`. Also add code-comments to to clarify the current behavior. [0]: b308b360ebc7f6639b9f75a6c4cb469115fe2f3e
This commit is contained in:
parent
6d7b0c56c9
commit
1b0cef86fa
@ -42,6 +42,9 @@ const char *GHOST_SystemPathsUnix::getSystemDir(int /*version*/, const char *ver
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* See doc-string & code-comments for #BLI_dir_home which matches this functionality.
|
||||
*/
|
||||
static const char *home_dir_get()
|
||||
{
|
||||
const char *home_dir = getenv("HOME");
|
||||
@ -128,7 +131,10 @@ const char *GHOST_SystemPathsUnix::getUserSpecialDir(GHOST_TUserSpecialDirTypes
|
||||
}
|
||||
|
||||
/* If `XDG_CACHE_HOME` is not set, then `$HOME/.cache is used`. */
|
||||
const char *home_dir = getenv("HOME");
|
||||
const char *home_dir = home_dir_get();
|
||||
if (home_dir == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
path = string(home_dir) + "/.cache";
|
||||
return path.c_str();
|
||||
}
|
||||
|
@ -230,8 +230,14 @@ char *BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT
|
||||
|
||||
/**
|
||||
* Get the user's home directory, i.e.
|
||||
* - Unix: `$HOME`
|
||||
* - Unix: `$HOME` or #passwd::pw_dir.
|
||||
* - Windows: `%userprofile%`
|
||||
*
|
||||
* \return The home directory or null when it cannot be accessed.
|
||||
*
|
||||
* \note By convention, failure to access home means any derived directories fail as well
|
||||
* instead of attempting to create a fallback such as `/`, `/tmp`, `C:\` ... etc.
|
||||
* Although there may be rare cases where a fallback is appropriate.
|
||||
*/
|
||||
const char *BLI_dir_home(void);
|
||||
|
||||
|
@ -114,6 +114,12 @@ const char *BLI_dir_home()
|
||||
#ifdef WIN32
|
||||
home_dir = BLI_getenv("userprofile");
|
||||
#else
|
||||
/* Return the users home directory with a fallback when the environment variable isn't set.
|
||||
* Failure to access `$HOME` is rare but possible, see: #2931.
|
||||
*
|
||||
* Any errors accessing home is likely caused by a broken/unsupported configuration,
|
||||
* nevertheless, failing to null check would crash which makes the error difficult
|
||||
* for users troubleshoot. */
|
||||
home_dir = BLI_getenv("HOME");
|
||||
if (home_dir == nullptr) {
|
||||
if (const passwd *pwuser = getpwuid(getuid())) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user