diff options
author | Tobias Stoeckmann <tobias@stoeckmann.org> | 2021-07-23 19:02:33 +0200 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2021-07-23 20:05:16 +0200 |
commit | e7f4e50da0a46babf968c266250df1f2a09f620f (patch) | |
tree | c639a1d298d77549005b0fc54b06dca9bfdc2d9a /sway | |
parent | Add `gaps <type> <scope> toggle <px>` command (diff) | |
download | sway-e7f4e50da0a46babf968c266250df1f2a09f620f.tar.gz sway-e7f4e50da0a46babf968c266250df1f2a09f620f.tar.zst sway-e7f4e50da0a46babf968c266250df1f2a09f620f.zip |
Fix crash when starting without HOME
If HOME environment variable is not set, sway fails startup with a
segmentation fault due to null pointer dereference.
Also check calloc return value and only perform the fallback code when
really needed.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Diffstat (limited to 'sway')
-rw-r--r-- | sway/config.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/sway/config.c b/sway/config.c index 39013865..fde386c7 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -354,12 +354,14 @@ static char *config_path(const char *prefix, const char *config_folder) { | |||
354 | static char *get_config_path(void) { | 354 | static char *get_config_path(void) { |
355 | char *path = NULL; | 355 | char *path = NULL; |
356 | const char *home = getenv("HOME"); | 356 | const char *home = getenv("HOME"); |
357 | size_t size_fallback = 1 + strlen(home) + strlen("/.config"); | 357 | char *config_home_fallback = NULL; |
358 | char *config_home_fallback = calloc(size_fallback, sizeof(char)); | ||
359 | snprintf(config_home_fallback, size_fallback, "%s/.config", home); | ||
360 | 358 | ||
361 | const char *config_home = getenv("XDG_CONFIG_HOME"); | 359 | const char *config_home = getenv("XDG_CONFIG_HOME"); |
362 | if (config_home == NULL || config_home[0] == '\0') { | 360 | if ((config_home == NULL || config_home[0] == '\0') && home != NULL) { |
361 | size_t size_fallback = 1 + strlen(home) + strlen("/.config"); | ||
362 | config_home_fallback = calloc(size_fallback, sizeof(char)); | ||
363 | if (config_home_fallback != NULL) | ||
364 | snprintf(config_home_fallback, size_fallback, "%s/.config", home); | ||
363 | config_home = config_home_fallback; | 365 | config_home = config_home_fallback; |
364 | } | 366 | } |
365 | 367 | ||