From 0b8aa86ecbec930ef0315bc4627c0f48966769d2 Mon Sep 17 00:00:00 2001 From: Gaël PORTAY Date: Sun, 21 Oct 2018 18:37:45 -0400 Subject: print_cpu_usage: fix invalid %cpu placeholder output Currently, the module cpu_usage prints %cpu0 information for the invalid %cpu placeholder (i.e. the cpu number is missing). Consider the following configuration. order += "cpu_usage" cpu_usage { format = "cpu0=%cpu0 cpu1=%cpu1 cpu=%cpu" # missing cpu number -------------------^ } The configuration above produces the output below. $ i3status -c config i3status: trying to auto-detect output_format setting i3status: auto-detected "term" cpu0=-2% cpu1=-49% cpu=-2% cpu0=06% cpu1=02% cpu=06% cpu0=05% cpu1=06% cpu=05% ... The module prints %cpu0 at the third placeholder where it should report an error. This commit fixes this behavior by initializing `number' to -1. If the cpu is missing in %cpu placeholder, the sscanf function does not set `number'. Because `number' is -1 (lower to 0), an error is reported and the placeholder is skipped. $ i3status -c ./config i3status: trying to auto-detect output_format setting i3status: auto-detected "term" provided CPU number '-1' above detected number of CPU 4 cpu0= cpu1=-48% cpu= provided CPU number '-1' above detected number of CPU 4 cpu0= cpu1=11% cpu= provided CPU number '-1' above detected number of CPU 4 cpu0= cpu1=03% cpu= ... --- src/print_cpu_usage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index 1fccba4..411d5f4 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -151,7 +151,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const } #if defined(LINUX) else if (BEGINS_WITH(walk + 1, "cpu")) { - int number = 0; + int number = -1; sscanf(walk + 1, "cpu%d", &number); if (number < 0 || number >= cpu_count) { fprintf(stderr, "provided CPU number '%d' above detected number of CPU %d\n", number, cpu_count); -- cgit v1.2.3 From aa572d24b5ecf16a064f924548ed1926b7b231d6 Mon Sep 17 00:00:00 2001 From: Gaël PORTAY Date: Tue, 23 Oct 2018 10:58:29 -0400 Subject: Add testcase for invalid %cpu placeholder --- testcases/024-cpu-usage-invalid-cpu/expected_output.pl | 7 +++++++ testcases/024-cpu-usage-invalid-cpu/i3status.conf | 9 +++++++++ 2 files changed, 16 insertions(+) create mode 100644 testcases/024-cpu-usage-invalid-cpu/expected_output.pl create mode 100644 testcases/024-cpu-usage-invalid-cpu/i3status.conf diff --git a/testcases/024-cpu-usage-invalid-cpu/expected_output.pl b/testcases/024-cpu-usage-invalid-cpu/expected_output.pl new file mode 100644 index 0000000..45111a6 --- /dev/null +++ b/testcases/024-cpu-usage-invalid-cpu/expected_output.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use v5.10; +use strict; +use warnings; + +print "CPU: \n"; diff --git a/testcases/024-cpu-usage-invalid-cpu/i3status.conf b/testcases/024-cpu-usage-invalid-cpu/i3status.conf new file mode 100644 index 0000000..d32784f --- /dev/null +++ b/testcases/024-cpu-usage-invalid-cpu/i3status.conf @@ -0,0 +1,9 @@ +general { + output_format = "none" +} + +order += "cpu_usage" + +cpu_usage { + format = "CPU: %cpu" +} -- cgit v1.2.3