Commit a3d90e27 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Do not assume sysconf() returns a positive value

parent 191f0716
...@@ -72,22 +72,22 @@ static char *config_GetHomeDir (void) ...@@ -72,22 +72,22 @@ static char *config_GetHomeDir (void)
{ {
/* 1/ Try $HOME */ /* 1/ Try $HOME */
const char *home = getenv ("HOME"); const char *home = getenv ("HOME");
if (home != NULL)
return strdup (home);
#if defined(HAVE_GETPWUID_R) #if defined(HAVE_GETPWUID_R)
/* 2/ Try /etc/passwd */ /* 2/ Try /etc/passwd */
char buf[sysconf (_SC_GETPW_R_SIZE_MAX)]; long max = sysconf (_SC_GETPW_R_SIZE_MAX);
if (home == NULL) if (max != -1)
{ {
struct passwd pw, *res; char buf[max];
struct passwd pwbuf, *pw;
if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res) if (getpwuid_r (getuid (), &pwbuf, buf, sizeof (buf), &pw) == 0
home = pw.pw_dir; && pw != NULL)
return strdup (pw->pw_dir);
} }
#endif #endif
if (!home)
return NULL; return NULL;
return strdup (home);
} }
static char *config_GetAppDir (const char *xdg_name, const char *xdg_default) static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment