config_libconfig: fix freeing of invalid pointer

xdgh in xdg_config_home is not always allocated. This commit makes sure
it's always a valid pointer to free by strdup it.

With helps from @dtzWill

Fixes #326
Closes #327

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2020-03-06 05:40:37 +00:00
parent 2f0b7cd992
commit 7eb9ee70a1
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
1 changed files with 8 additions and 3 deletions

View File

@ -39,14 +39,19 @@ static inline int lcfg_lookup_bool(const config_t *config, const char *path, boo
const char *xdg_config_home(void) {
char *xdgh = getenv("XDG_CONFIG_HOME");
char *home = getenv("HOME");
const char *default_dir = "/.config";
if (!xdgh) {
if (!home) return NULL;
if (!home) {
return NULL;
}
xdgh = cvalloc(strlen(home) + 8 + 1);
xdgh = cvalloc(strlen(home) + strlen(default_dir) + 1);
strcpy(xdgh, home);
strcat(xdgh, "/.config");
strcat(xdgh, default_dir);
} else {
xdgh = strdup(xdgh);
}
return xdgh;