From 9c9ee3e4ef9d4e57ec801a79e0b2da1bd2a6d46e Mon Sep 17 00:00:00 2001
From: Dominique Martinet <asmadeus@codewreck.org>
Date: Sat, 30 Jun 2018 22:39:37 +0900
Subject: find prev/next output/workspace: add NULL check

These could be called with NULL if there is no focus

Found through static analysis.
---
 sway/tree/workspace.c | 6 ++++++
 1 file changed, 6 insertions(+)

(limited to 'sway/tree/workspace.c')

diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
index 5eb4be0f..3a311cd1 100644
--- a/sway/tree/workspace.c
+++ b/sway/tree/workspace.c
@@ -271,6 +271,9 @@ struct sway_container *workspace_by_name(const char *name) {
  */
 struct sway_container *workspace_output_prev_next_impl(
 		struct sway_container *output, bool next) {
+	if (!output) {
+		return NULL;
+	}
 	if (!sway_assert(output->type == C_OUTPUT,
 				"Argument must be an output, is %d", output->type)) {
 		return NULL;
@@ -303,6 +306,9 @@ struct sway_container *workspace_output_prev_next_impl(
  */
 struct sway_container *workspace_prev_next_impl(
 		struct sway_container *workspace, bool next) {
+	if (!workspace) {
+		return NULL;
+	}
 	if (!sway_assert(workspace->type == C_WORKSPACE,
 				"Argument must be a workspace, is %d", workspace->type)) {
 		return NULL;
-- 
cgit v1.2.3


From c78ab67877e15e4bbbdfd4e8bb7f94309980489b Mon Sep 17 00:00:00 2001
From: Dominique Martinet <asmadeus@codewreck.org>
Date: Sun, 1 Jul 2018 22:46:48 +0900
Subject: 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.
---
 sway/tree/workspace.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

(limited to 'sway/tree/workspace.c')

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,
 char *workspace_next_name(const char *output_name) {
 	wlr_log(L_DEBUG, "Workspace: Generating new workspace name for output %s",
 			output_name);
-	int l = 1;
 	// Scan all workspace bindings to find the next available workspace name,
 	// if none are found/available then default to a number
 	struct sway_mode *mode = config->current_mode;
@@ -202,14 +201,9 @@ char *workspace_next_name(const char *output_name) {
 	// As a fall back, get the current number of active workspaces
 	// and return that + 1 for the next workspace's name
 	int ws_num = root_container.children->length;
-	if (ws_num >= 10) {
-		l = 2;
-	} else if (ws_num >= 100) {
-		l = 3;
-	}
+	int l = snprintf(NULL, 0, "%d", ws_num);
 	char *name = malloc(l + 1);
-	if (!name) {
-		wlr_log(L_ERROR, "Could not allocate workspace name");
+	if (!sway_assert(name, "Cloud not allocate workspace name")) {
 		return NULL;
 	}
 	sprintf(name, "%d", ws_num++);
-- 
cgit v1.2.3