From 63ef659225aaaf14f22c76ef74c18acba1bb0cee Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 9 Jul 2016 14:52:54 +1000 Subject: [PATCH] watch: Don't process additional numbers in unknown ANSI color escapes process_ansi assumed all numbers in a color control sequence correspond to colors or attributes, which breaks badly if it encounters a ISO-8613-3 escape sequence (such as for truecolor RGB). For instance, the sequence "\x1b[38;2;10;20;30m" sets the foreground color to rgb(10,20,30), but watch will interpret all five numbers in the sequence as colors or attributes themselves. Stop processing the entire escape sequence if watch encounters any number it doesn't understand, as that number may change the meaning of the rest of the sequence. --- watch.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/watch.c b/watch.c index 7d422233..17608b5c 100644 --- a/watch.c +++ b/watch.c @@ -136,7 +136,7 @@ static void init_ansi_colors(void) } -static void set_ansi_attribute(const int attrib) +static int set_ansi_attribute(const int attrib) { switch (attrib) { case -1: /* restore last settings */ @@ -189,8 +189,11 @@ static void set_ansi_attribute(const int attrib) fg_col = attrib - 30 + 1; } else if (attrib >= 40 && attrib <= 47) { /* set background color */ bg_col = attrib - 40 + 1; + } else { + return 0; /* Not understood */ } } + return 1; attrset(attributes | COLOR_PAIR(bg_col * nr_of_colors + fg_col + 1)); } @@ -233,7 +236,8 @@ static void process_ansi(FILE * fp) set_ansi_attribute(0); for (endptr = numstart = buf; *endptr != '\0'; numstart = endptr + 1) { - set_ansi_attribute(strtol(numstart, &endptr, 10)); + if (!set_ansi_attribute(strtol(numstart, &endptr, 10))) + break; if (numstart == endptr) set_ansi_attribute(0); /* [m treated as [0m */ }