diff options
author | 2018-06-26 12:57:22 +0200 | |
---|---|---|
committer | 2018-06-26 15:40:32 +0200 | |
commit | a4578815f1fa30a7ebb15ddb6601f1ab2f3a3fb6 (patch) | |
tree | d51ff97e2ec91b83b1af0423da046b31b07ea161 /sway | |
parent | fix memleak on background cmd error (diff) | |
download | sway-a4578815f1fa30a7ebb15ddb6601f1ab2f3a3fb6.tar.gz sway-a4578815f1fa30a7ebb15ddb6601f1ab2f3a3fb6.tar.zst sway-a4578815f1fa30a7ebb15ddb6601f1ab2f3a3fb6.zip |
cleanup output-background subcommand handling
- fixes a double-free error when access() failed.
- refactor code to make memory managment (alloc/free) more straightforward
- do not bring the temporary wordexp_t struct around
- do not postpone errors handling
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands/output/background.c | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c index 4f422cec..55cbdff0 100644 --- a/sway/commands/output/background.c +++ b/sway/commands/output/background.c | |||
@@ -69,42 +69,49 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { | |||
69 | return cmd_res; | 69 | return cmd_res; |
70 | } | 70 | } |
71 | free(src); | 71 | free(src); |
72 | src = p.we_wordv[0]; | 72 | src = strdup(p.we_wordv[0]); |
73 | wordfree(&p); | ||
74 | if (!src) { | ||
75 | wlr_log(L_ERROR, "Failed to duplicate string"); | ||
76 | return cmd_results_new(CMD_FAILURE, "output", | ||
77 | "Unable to allocate resource"); | ||
78 | } | ||
79 | |||
73 | if (config->reading && *src != '/') { | 80 | if (config->reading && *src != '/') { |
81 | // src file is inside configuration dir | ||
82 | |||
74 | char *conf = strdup(config->current_config); | 83 | char *conf = strdup(config->current_config); |
75 | if (conf) { | 84 | if(!conf) { |
76 | char *conf_path = dirname(conf); | 85 | wlr_log(L_ERROR, "Failed to duplicate string"); |
77 | src = malloc(strlen(conf_path) + strlen(src) + 2); | 86 | return cmd_results_new(CMD_FAILURE, "output", |
78 | if (!src) { | ||
79 | free(conf); | ||
80 | wordfree(&p); | ||
81 | wlr_log(L_ERROR, | ||
82 | "Unable to allocate resource: Not enough memory"); | ||
83 | return cmd_results_new(CMD_FAILURE, "output", | ||
84 | "Unable to allocate resources"); | 87 | "Unable to allocate resources"); |
85 | } | 88 | } |
86 | sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); | 89 | |
90 | char *conf_path = dirname(conf); | ||
91 | char *rel_path = src; | ||
92 | src = malloc(strlen(conf_path) + strlen(src) + 2); | ||
93 | if (!src) { | ||
94 | free(rel_path); | ||
87 | free(conf); | 95 | free(conf); |
88 | } else { | 96 | wlr_log(L_ERROR, "Unable to allocate memory"); |
89 | wlr_log(L_ERROR, "Unable to allocate background source"); | 97 | return cmd_results_new(CMD_FAILURE, "output", |
98 | "Unable to allocate resources"); | ||
90 | } | 99 | } |
100 | |||
101 | sprintf(src, "%s/%s", conf_path, rel_path); | ||
102 | free(rel_path); | ||
103 | free(conf); | ||
91 | } | 104 | } |
92 | 105 | ||
93 | if (access(src, F_OK) == -1) { | 106 | if (access(src, F_OK) == -1) { |
94 | struct cmd_results *cmd_res = cmd_results_new(CMD_FAILURE, "output", | 107 | struct cmd_results *cmd_res = cmd_results_new(CMD_FAILURE, "output", |
95 | "Unable to access background file '%s': %s", src, strerror(errno)); | 108 | "Unable to access background file '%s': %s", src, strerror(errno)); |
96 | free(src); | 109 | free(src); |
97 | wordfree(&p); | ||
98 | return cmd_res; | 110 | return cmd_res; |
99 | } | 111 | } |
100 | 112 | ||
101 | output->background = strdup(src); | 113 | output->background = src; |
102 | output->background_option = strdup(mode); | 114 | output->background_option = strdup(mode); |
103 | if (src != p.we_wordv[0]) { | ||
104 | free(src); | ||
105 | } | ||
106 | wordfree(&p); | ||
107 | |||
108 | argc -= j + 1; argv += j + 1; | 115 | argc -= j + 1; argv += j + 1; |
109 | } | 116 | } |
110 | 117 | ||