From d35de66f1e35f6c6ad6df5ad33c44541e19a5cdf Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Tue, 12 Sep 2017 12:29:01 +0300 Subject: [PATCH] scalloc parse_config input to make sure it terminates with '\0' Otherwise strchr() can crash for files that don't end with '\n' because it won't find a null char to terminate at. Fixes #2934 --- src/config_parser.c | 2 +- testcases/t/270-config-no-newline-end.t | 41 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 testcases/t/270-config-no-newline-end.t diff --git a/src/config_parser.c b/src/config_parser.c index c88e9d1e..58a5552c 100644 --- a/src/config_parser.c +++ b/src/config_parser.c @@ -1025,7 +1025,7 @@ bool parse_file(const char *f, bool use_nagbar) { /* Then, allocate a new buffer and copy the file over to the new one, * but replace occurrences of our variables */ char *walk = buf, *destwalk; - char *new = smalloc(stbuf.st_size + extra_bytes + 1); + char *new = scalloc(stbuf.st_size + extra_bytes + 1, 1); destwalk = new; while (walk < (buf + stbuf.st_size)) { /* Find the next variable */ diff --git a/testcases/t/270-config-no-newline-end.t b/testcases/t/270-config-no-newline-end.t new file mode 100644 index 00000000..4e7ccbf7 --- /dev/null +++ b/testcases/t/270-config-no-newline-end.t @@ -0,0 +1,41 @@ +#!perl +# vim:ts=4:sw=4:expandtab +# +# Please read the following documents before working on tests: +# • http://build.i3wm.org/docs/testsuite.html +# (or docs/testsuite) +# +# • http://build.i3wm.org/docs/lib-i3test.html +# (alternatively: perldoc ./testcases/lib/i3test.pm) +# +# • http://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) +# +# Make sure that configs that end without a newline don't crash i3. +# Ticket: #2934 +use i3test i3_autostart => 0; + +my $first_lines = <<'EOT'; +set $workspace1 workspace number 1 +set $workspace0 workspace eggs + +bindsym Mod4+1 $workspace1 +EOT + +# Intentionally don't add a trailing newline for the last line since this is +# what triggered the bug. +my $last_line = 'bindsym Mod4+0 $workspace0'; +my $config = "${first_lines}${last_line}"; + +my $pid = launch_with_config($config); +does_i3_live; + +my $i3 = i3(get_socket_path()); +my $ws = $i3->get_workspaces->recv; +is($ws->[0]->{name}, 'eggs', 'last line processed correctly'); + +exit_gracefully($pid); +done_testing; -- 2.39.5