From 01ba240272477241cbb8ad1f1aa28c86634b4049 Mon Sep 17 00:00:00 2001
From: Jeff Smith <toxicglados@gmail.com>
Date: Tue, 24 Aug 2021 16:34:30 -0500
Subject: [PATCH] Fix bug where long commands crash i3

Commands with more than 10 words would result in i3 crashing. The root
cause is that the stack which holds arguments with identifiers is
only 10 big, but generate-command-parser.pl was giving every word an
identifier of "".
---
 generate-command-parser.pl      | 11 ++++++--
 testcases/t/315-long-commands.t | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)
 create mode 100644 testcases/t/315-long-commands.t

diff --git a/generate-command-parser.pl b/generate-command-parser.pl
index cef4eda5..17728736 100755
--- a/generate-command-parser.pl
+++ b/generate-command-parser.pl
@@ -228,8 +228,15 @@ for my $state (@keys) {
             ($call_identifier) = ($next_state =~ /^call ([0-9]+)$/);
             $next_state = '__CALL';
         }
-        my $identifier = $token->{identifier};
-        say $tokfh qq|    { "$token_name", "$identifier", $next_state, { $call_identifier } },|;
+        my $identifier;
+        # Set $identifier to NULL if there is no identifier
+        if ($token->{identifier} eq ""){
+            $identifier = "NULL"
+        }
+        else{
+            $identifier = qq|"$token->{identifier}"|;
+        }
+        say $tokfh qq|    { "$token_name", $identifier, $next_state, { $call_identifier } },|;
     }
     say $tokfh '};';
 }
diff --git a/testcases/t/315-long-commands.t b/testcases/t/315-long-commands.t
new file mode 100644
index 00000000..3db37dab
--- /dev/null
+++ b/testcases/t/315-long-commands.t
@@ -0,0 +1,46 @@
+#!perl
+# vim:ts=4:sw=4:expandtab
+#
+# Please read the following documents before working on tests:
+# • https://build.i3wm.org/docs/testsuite.html
+#   (or docs/testsuite)
+#
+# • https://build.i3wm.org/docs/lib-i3test.html
+#   (alternatively: perldoc ./testcases/lib/i3test.pm)
+#
+# • https://build.i3wm.org/docs/ipc.html
+#   (or docs/ipc)
+#
+# • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
+#   (unless you are already familiar with Perl)
+#
+# Test that commands with more than 10 non-identified words doesn't works
+# 10 is the magic number chosen for the stack size which is why it's used here
+# Ticket: #2968
+# Bug still in: 4.19.2-103-gfc65ca36
+use i3test;
+
+######################################################################
+# 1) run a long command
+######################################################################
+
+my $i3 = i3(get_socket_path());
+my $tmp = fresh_workspace;
+
+my $floatwin = open_floating_window;
+
+
+my ($absolute_before, $top_before) = $floatwin->rect;
+
+cmd 'move window container to window container to window container to left';
+
+sync_with_i3;
+
+my ($absolute, $top) = $floatwin->rect;
+
+is($absolute->x, ($absolute_before->x - 10), 'moved 10 px to the left');
+is($absolute->y, $absolute_before->y, 'y not changed');
+is($absolute->width, $absolute_before->width, 'width not changed');
+is($absolute->height, $absolute_before->height, 'height not changed');
+
+done_testing;