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:
parent
2f0b7cd992
commit
7eb9ee70a1
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue