From cacb45fbcdf74d72288b252fc557480906117d24 Mon Sep 17 00:00:00 2001 From: Maxim Solovyov Date: Fri, 22 Dec 2023 01:07:07 +0300 Subject: [PATCH] core: fix segfaults when the HOME environment variable is not set --- src/config.c | 12 +++++++----- src/config_libconfig.c | 14 ++++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/config.c b/src/config.c index ec39aa4390..f5764c5c39 100644 --- a/src/config.c +++ b/src/config.c @@ -596,15 +596,17 @@ char *locate_auxiliary_file(const char *scope, const char *path, const char *inc // Fall back to searching in user config directory scoped_charp picom_scope = mstrjoin("/picom/", scope); scoped_charp config_home = (char *)xdg_config_home(); - char *ret = locate_auxiliary_file_at(config_home, picom_scope, path); - if (ret) { - return ret; + if (config_home) { + char *ret = locate_auxiliary_file_at(config_home, picom_scope, path); + if (ret) { + return ret; + } } // Fall back to searching in system config directory auto config_dirs = xdg_config_dirs(); for (int i = 0; config_dirs[i]; i++) { - ret = locate_auxiliary_file_at(config_dirs[i], picom_scope, path); + char *ret = locate_auxiliary_file_at(config_dirs[i], picom_scope, path); if (ret) { free(config_dirs); return ret; @@ -612,7 +614,7 @@ char *locate_auxiliary_file(const char *scope, const char *path, const char *inc } free(config_dirs); - return ret; + return NULL; } /** diff --git a/src/config_libconfig.c b/src/config_libconfig.c index f2a8e07faa..ed2097eb76 100644 --- a/src/config_libconfig.c +++ b/src/config_libconfig.c @@ -81,17 +81,19 @@ FILE *open_config_file(const char *cpath, char **ppath) { // First search for config file in user config directory auto config_home = xdg_config_home(); - auto ret = open_config_file_at(config_home, ppath); - free((void *)config_home); - if (ret) { - return ret; + if (config_home) { + auto ret = open_config_file_at(config_home, ppath); + free((void *)config_home); + if (ret) { + return ret; + } } // Fall back to legacy config file in user home directory const char *home = getenv("HOME"); if (home && strlen(home)) { auto path = mstrjoin(home, config_filename_legacy); - ret = fopen(path, "r"); + auto ret = fopen(path, "r"); if (ret && ppath) { *ppath = path; } else { @@ -105,7 +107,7 @@ FILE *open_config_file(const char *cpath, char **ppath) { // Fall back to config file in system config directory auto config_dirs = xdg_config_dirs(); for (int i = 0; config_dirs[i]; i++) { - ret = open_config_file_at(config_dirs[i], ppath); + auto ret = open_config_file_at(config_dirs[i], ppath); if (ret) { free(config_dirs); return ret;