mirror of https://github.com/OpenTTD/OpenTTD
(svn r26114) -Fix-ish: do our best to not get bit by getenv being unsafe as hell
parent
43f76dcabb
commit
d2ffba07bb
|
@ -1097,20 +1097,33 @@ void DetermineBasePaths(const char *exe)
|
||||||
#ifdef __HAIKU__
|
#ifdef __HAIKU__
|
||||||
BPath path;
|
BPath path;
|
||||||
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
||||||
const char *homedir = path.Path();
|
const char *homedir = strdup(path.Path());
|
||||||
#else
|
#else
|
||||||
|
/* getenv is highly unsafe; duplicate it as soon as possible,
|
||||||
|
* or at least before something else touches the environment
|
||||||
|
* variables in any way. It can also contain all kinds of
|
||||||
|
* unvalidated data we rather not want internally. */
|
||||||
const char *homedir = getenv("HOME");
|
const char *homedir = getenv("HOME");
|
||||||
|
if (homedir != NULL) {
|
||||||
|
homedir = strndup(homedir, MAX_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
if (homedir == NULL) {
|
if (homedir == NULL) {
|
||||||
const struct passwd *pw = getpwuid(getuid());
|
const struct passwd *pw = getpwuid(getuid());
|
||||||
homedir = (pw == NULL) ? "" : pw->pw_dir;
|
homedir = (pw == NULL) ? NULL : strdup(pw->pw_dir);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
if (homedir != NULL) {
|
||||||
AppendPathSeparator(tmp, MAX_PATH);
|
ValidateString(homedir);
|
||||||
|
snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
||||||
|
AppendPathSeparator(tmp, MAX_PATH);
|
||||||
|
|
||||||
_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
|
_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
|
||||||
|
free(homedir);
|
||||||
|
} else {
|
||||||
|
_searchpaths[SP_PERSONAL_DIR] = NULL;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(WITH_SHARED_DIR)
|
#if defined(WITH_SHARED_DIR)
|
||||||
|
|
Loading…
Reference in New Issue