aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Safin <xzfcpw@gmail.com>2020-02-21 02:07:04 +0000
committerAlbert Safin <xzfcpw@gmail.com>2020-02-21 02:07:04 +0000
commit83c7aff089a6728b6e522d934d656a8e09463112 (patch)
tree782ebb2f008b4ac6482f27a50072273820161ed8
parentd3976fee8c5efb5b8544366f7b221c5cf08aa40c (diff)
downloadi3-83c7aff089a6728b6e522d934d656a8e09463112.tar.gz
i3-83c7aff089a6728b6e522d934d656a8e09463112.zip
Limit workspace numbers within 0..INT32_MAX
Before this commit, large workspace numbers treated oddly: $ i3-msg 'rename workspace to 1234567890' # displayed in i3bar as `0` $ i3-msg 'rename workspace to 4294967200' $ i3-msg -t get_workspaces | jq '.[]|select(.focused).num' -96 # int32_t overflow $ i3-msg 'rename workspace to 99999999999999999999' $ i3-msg -t get_workspaces | jq '.[]|select(.focused).num' -1 # treated as unnumbered This commit puts a consistent limit on workspace numbers. Now workspaces with numbers beyond INT32_MAX are treated as unnumbered.
-rw-r--r--i3bar/src/workspaces.c2
-rw-r--r--include/util.h2
-rw-r--r--src/util.c10
-rw-r--r--testcases/t/117-workspace.t15
4 files changed, 21 insertions, 8 deletions
diff --git a/i3bar/src/workspaces.c b/i3bar/src/workspaces.c
index 5b43c4a0..68686611 100644
--- a/i3bar/src/workspaces.c
+++ b/i3bar/src/workspaces.c
@@ -114,7 +114,7 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, size_t
if ((config.strip_ws_numbers || config.strip_ws_name) && params->workspaces_walk->num >= 0) {
/* Special case: strip off the workspace number/name */
- static char ws_num[10];
+ static char ws_num[32];
snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num);
diff --git a/include/util.h b/include/util.h
index 8459db10..f0db03a5 100644
--- a/include/util.h
+++ b/include/util.h
@@ -86,7 +86,7 @@ bool layout_from_name(const char *layout_str, layout_t *out);
* interpreted as a "named workspace".
*
*/
-long ws_name_to_number(const char *name);
+int ws_name_to_number(const char *name);
/**
* Updates *destination with new_value and returns true if it was changed or false
diff --git a/src/util.c b/src/util.c
index 969dc2a2..3544297c 100644
--- a/src/util.c
+++ b/src/util.c
@@ -109,14 +109,12 @@ bool layout_from_name(const char *layout_str, layout_t *out) {
* interpreted as a "named workspace".
*
*/
-long ws_name_to_number(const char *name) {
+int ws_name_to_number(const char *name) {
/* positive integers and zero are interpreted as numbers */
char *endptr = NULL;
- long parsed_num = strtol(name, &endptr, 10);
- if (parsed_num == LONG_MIN ||
- parsed_num == LONG_MAX ||
- parsed_num < 0 ||
- endptr == name) {
+ errno = 0;
+ long long parsed_num = strtoll(name, &endptr, 10);
+ if (errno != 0 || parsed_num > INT32_MAX || parsed_num < 0 || endptr == name) {
parsed_num = -1;
}
diff --git a/testcases/t/117-workspace.t b/testcases/t/117-workspace.t
index c97d4fbf..48a0dbf6 100644
--- a/testcases/t/117-workspace.t
+++ b/testcases/t/117-workspace.t
@@ -130,6 +130,21 @@ $ws = get_ws("aa: $tmp");
ok(defined($ws), "workspace aa: $tmp was created");
is($ws->{num}, -1, 'workspace number is -1');
+cmd "workspace -42: $tmp";
+$ws = get_ws("-42: $tmp");
+ok(defined($ws), "workspace -42: $tmp was created");
+is($ws->{num}, -1, 'negative workspace number is ignored');
+
+cmd "workspace 2147483647: $tmp";
+$ws = get_ws("2147483647: $tmp");
+ok(defined($ws), "workspace 2147483647: $tmp was created");
+is($ws->{num}, 2147483647, 'workspace number is 2147483647');
+
+cmd "workspace 2147483648: $tmp";
+$ws = get_ws("2147483648: $tmp");
+ok(defined($ws), "workspace 2147483648: $tmp was created");
+is($ws->{num}, -1, 'workspace number past the limit is ignored');
+
################################################################################
# Check that we can go to workspace "4: foo" with the command
# "workspace number 4".