Skip to content

Commit

Permalink
core: fix segfaults when the HOME environment variable is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelynothelix committed Dec 22, 2023
1 parent b368072 commit cacb45f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,23 +596,25 @@ 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;

Check warning on line 602 in src/config.c

View check run for this annotation

Codecov / codecov/patch

src/config.c#L599-L602

Added lines #L599 - L602 were not covered by tests
}
}

// 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);

Check warning on line 609 in src/config.c

View check run for this annotation

Codecov / codecov/patch

src/config.c#L609

Added line #L609 was not covered by tests
if (ret) {
free(config_dirs);
return ret;
}
}
free(config_dirs);

return ret;
return NULL;

Check warning on line 617 in src/config.c

View check run for this annotation

Codecov / codecov/patch

src/config.c#L617

Added line #L617 was not covered by tests
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/config_libconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning on line 88 in src/config_libconfig.c

View check run for this annotation

Codecov / codecov/patch

src/config_libconfig.c#L84-L88

Added lines #L84 - L88 were not covered by tests
}
}

// 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");

Check warning on line 96 in src/config_libconfig.c

View check run for this annotation

Codecov / codecov/patch

src/config_libconfig.c#L96

Added line #L96 was not covered by tests
if (ret && ppath) {
*ppath = path;
} else {
Expand All @@ -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);

Check warning on line 110 in src/config_libconfig.c

View check run for this annotation

Codecov / codecov/patch

src/config_libconfig.c#L110

Added line #L110 was not covered by tests
if (ret) {
free(config_dirs);
return ret;
Expand Down

0 comments on commit cacb45f

Please sign in to comment.