From 7eb9ee70a131b0062e95879acabaa541fdfe8aa2 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Fri, 6 Mar 2020 05:40:37 +0000 Subject: [PATCH] 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 --- src/config_libconfig.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/config_libconfig.c b/src/config_libconfig.c index c2fb243..6cce394 100644 --- a/src/config_libconfig.c +++ b/src/config_libconfig.c @@ -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;