diff options
author | Dominique Martinet <asmadeus@codewreck.org> | 2018-07-01 22:46:48 +0900 |
---|---|---|
committer | Dominique Martinet <asmadeus@codewreck.org> | 2018-07-02 08:03:41 +0900 |
commit | c78ab67877e15e4bbbdfd4e8bb7f94309980489b (patch) | |
tree | 8811560930cc848dee2044aa729e4060c10e75e0 | |
parent | find prev/next output/workspace: add NULL check (diff) | |
download | sway-c78ab67877e15e4bbbdfd4e8bb7f94309980489b.tar.gz sway-c78ab67877e15e4bbbdfd4e8bb7f94309980489b.tar.zst sway-c78ab67877e15e4bbbdfd4e8bb7f94309980489b.zip |
workspace_next_name: fix string length for ws_num >= 100
The check didn't include && ws_num < 100 so l would always be 1 or 2
Instead of fixing logic it's simpler to just call snprintf twice to get
length and use that.
Also change malloc failure check to sway_assert because both callers of
this function do not do null check and would segfault...
Found through static analysis.
-rw-r--r-- | sway/tree/workspace.c | 10 |
1 files changed, 2 insertions, 8 deletions
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 3a311cd1..2db06a31 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c | |||
@@ -109,7 +109,6 @@ static bool workspace_valid_on_output(const char *output_name, | |||
109 | char *workspace_next_name(const char *output_name) { | 109 | char *workspace_next_name(const char *output_name) { |
110 | wlr_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", | 110 | wlr_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", |
111 | output_name); | 111 | output_name); |
112 | int l = 1; | ||
113 | // Scan all workspace bindings to find the next available workspace name, | 112 | // Scan all workspace bindings to find the next available workspace name, |
114 | // if none are found/available then default to a number | 113 | // if none are found/available then default to a number |
115 | struct sway_mode *mode = config->current_mode; | 114 | struct sway_mode *mode = config->current_mode; |
@@ -202,14 +201,9 @@ char *workspace_next_name(const char *output_name) { | |||
202 | // As a fall back, get the current number of active workspaces | 201 | // As a fall back, get the current number of active workspaces |
203 | // and return that + 1 for the next workspace's name | 202 | // and return that + 1 for the next workspace's name |
204 | int ws_num = root_container.children->length; | 203 | int ws_num = root_container.children->length; |
205 | if (ws_num >= 10) { | 204 | int l = snprintf(NULL, 0, "%d", ws_num); |
206 | l = 2; | ||
207 | } else if (ws_num >= 100) { | ||
208 | l = 3; | ||
209 | } | ||
210 | char *name = malloc(l + 1); | 205 | char *name = malloc(l + 1); |
211 | if (!name) { | 206 | if (!sway_assert(name, "Cloud not allocate workspace name")) { |
212 | wlr_log(L_ERROR, "Could not allocate workspace name"); | ||
213 | return NULL; | 207 | return NULL; |
214 | } | 208 | } |
215 | sprintf(name, "%d", ws_num++); | 209 | sprintf(name, "%d", ws_num++); |