Skip to content

Commit d75dfba

Browse files
committed
output/background: fix config ignoring fallback color
currently, the output background command handler prematurely returns with an error if the background file cannot be accessed. It should only error if user did not provide fallback color. closes #8556 Changes - Introduce variables to avoid uneccessary writing on output members - Log a debug message when fallback is being used over inaccessible file - Always parse the background color and swaynag warn if it is incorrect - when updating output member variables, free previous values - add cleanup label and goto it if `strdup` fails - Move output->member initializations to before parsing fallback, Also free and init output->background as well
1 parent d093c2e commit d75dfba

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

sway/commands/output/background.c

+38-44
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
#include <string.h>
44
#include <strings.h>
55
#include <unistd.h>
6-
#include <errno.h>
76
#include "sway/commands.h"
87
#include "sway/config.h"
9-
#include "sway/swaynag.h"
108
#include "log.h"
119
#include "stringop.h"
1210

@@ -42,14 +40,14 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
4240
}
4341

4442
struct output_config *output = config->handler_context.output_config;
45-
43+
char *src = NULL;
4644
if (strcasecmp(argv[1], "solid_color") == 0) {
4745
if (!validate_color(argv[0])) {
4846
return cmd_results_new(CMD_INVALID,
4947
"Colors should be of the form #RRGGBB");
5048
}
51-
output->background = strdup(argv[0]);
52-
output->background_option = strdup("solid_color");
49+
if (!(output->background = strdup(argv[0]))) goto cleanup;
50+
if (!(output->background_option = strdup("solid_color"))) goto cleanup;
5351
output->background_fallback = NULL;
5452
argc -= 2; argv += 2;
5553
} else {
@@ -77,37 +75,25 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
7775
return cmd_results_new(CMD_INVALID, "Missing background file");
7876
}
7977

80-
char *src = join_args(argv, j);
78+
if (!(src = join_args(argv, j))) goto cleanup;
8179
if (!expand_path(&src)) {
8280
struct cmd_results *cmd_res = cmd_results_new(CMD_INVALID,
8381
"Invalid syntax (%s)", src);
8482
free(src);
8583
return cmd_res;
8684
}
87-
if (!src) {
88-
sway_log(SWAY_ERROR, "Failed to allocate expanded path");
89-
return cmd_results_new(CMD_FAILURE, "Unable to allocate resource");
90-
}
9185

9286
if (config->reading && *src != '/') {
9387
// src file is inside configuration dir
9488

9589
char *conf = strdup(config->current_config_path);
96-
if (!conf) {
97-
sway_log(SWAY_ERROR, "Failed to duplicate string");
98-
free(src);
99-
return cmd_results_new(CMD_FAILURE,
100-
"Unable to allocate resources");
101-
}
90+
if (!conf) goto cleanup;
10291

10392
char *conf_path = dirname(conf);
10493
char *real_src = malloc(strlen(conf_path) + strlen(src) + 2);
10594
if (!real_src) {
106-
free(src);
10795
free(conf);
108-
sway_log(SWAY_ERROR, "Unable to allocate memory");
109-
return cmd_results_new(CMD_FAILURE,
110-
"Unable to allocate resources");
96+
goto cleanup;
11197
}
11298

11399
snprintf(real_src, strlen(conf_path) + strlen(src) + 2, "%s/%s", conf_path, src);
@@ -117,40 +103,48 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
117103
}
118104

119105
bool can_access = access(src, F_OK) != -1;
120-
if (!can_access) {
121-
sway_log_errno(SWAY_ERROR, "Unable to access background file '%s'",
122-
src);
123-
config_add_swaynag_warning("Unable to access background file '%s'",
124-
src);
125-
struct cmd_results *result = cmd_results_new(CMD_FAILURE,
126-
"unable to access background file '%s'", src);
127-
free(src);
128-
return result;
129-
} else {
130-
output->background = src;
131-
output->background_option = strdup(mode);
132-
}
133106
argc -= j + 1; argv += j + 1;
107+
free(output->background_option);
108+
free(output->background_fallback);
109+
free(output->background);
110+
output->background = output->background_option = output->background_fallback = NULL;
111+
char *fallback = NULL;
134112

135-
output->background_fallback = NULL;
136113
if (argc && *argv[0] == '#') {
137-
if (!validate_color(argv[0])) {
138-
return cmd_results_new(CMD_INVALID,
139-
"fallback color should be of the form #RRGGBB");
114+
if (validate_color(argv[0])) {
115+
if (!(fallback = strdup(argv[0]))) goto cleanup;
116+
output->background_fallback = fallback;
117+
} else {
118+
sway_log(SWAY_ERROR, "fallback '%s' should be of the form #RRGGBB", argv[0]);
119+
config_add_swaynag_warning("fallback '%s' should be of the form #RRGGBB\n", argv[0]);
140120
}
141-
142-
output->background_fallback = strdup(argv[0]);
143121
argc--; argv++;
122+
}
144123

145-
if (!can_access) {
146-
output->background = output->background_fallback;
147-
output->background_option = strdup("solid_color");
148-
output->background_fallback = NULL;
124+
if (!can_access) {
125+
if (!fallback) {
126+
sway_log(SWAY_ERROR, "Unable to access background file '%s' "
127+
"and no valid fallback provided", src);
128+
struct cmd_results *res = cmd_results_new(CMD_FAILURE, "Unable to access "
129+
"background file '%s' and no valid fallback provided", src);
130+
free(src);
131+
return res;
149132
}
133+
sway_log(SWAY_DEBUG, "Cannot access file '%s', using fallback '%s'", src, fallback);
134+
output->background = fallback;
135+
if (!(output->background_option = strdup("solid_color"))) goto cleanup;
136+
output->background_fallback = NULL;
137+
} else {
138+
output->background = src;
139+
if (!(output->background_option = strdup(mode))) goto cleanup;
150140
}
151141
}
152-
153142
config->handler_context.leftovers.argc = argc;
154143
config->handler_context.leftovers.argv = argv;
155144
return NULL;
145+
146+
cleanup:
147+
free(src);
148+
sway_log(SWAY_ERROR, "Failed to allocate resources");
149+
return cmd_results_new(CMD_FAILURE, "Unable to allocate resources");
156150
}

0 commit comments

Comments
 (0)