]> granicus.if.org Git - procps-ng/commitdiff
watch: Don't process additional numbers in unknown ANSI color escapes
authorJosh Triplett <josh@joshtriplett.org>
Sat, 9 Jul 2016 04:52:54 +0000 (14:52 +1000)
committerCraig Small <csmall@enc.com.au>
Sat, 9 Jul 2016 05:03:06 +0000 (15:03 +1000)
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

diff --git a/watch.c b/watch.c
index 7d42223380f08d1f890be9063f2ee200fc5aefb1..17608b5cb938531275a5c9997035e160a17f5031 100644 (file)
--- 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 */
     }