console: fix cell overflow
commit5b8541c6c70db776d0701bb5ce5862ae15779fb5
authorGerd Hoffmann <kraxel@redhat.com>
Mon, 1 Jul 2019 07:53:01 +0000 (1 09:53 +0200)
committerGerd Hoffmann <kraxel@redhat.com>
Wed, 3 Jul 2019 08:57:12 +0000 (3 10:57 +0200)
treec195553a940e80668bb38fe41d2c9bba954d2f0d
parent506179e42112be77bfd071f050b15762d3b2cd43
console: fix cell overflow

Linux terminal behavior (coming from vt100 I think) is somewhat strange
when it comes to line wraps:  When a character is printed to the last
char cell of a line the cursor does NOT jump to the next line but stays
where it is.  The line feed happens when the next character is printed.

So the valid range for the cursor position is not 0 .. width-1 but
0 .. width, where x == width represents the state where the line is
full but the cursor didn't jump to the next line yet.

The code for the 'clear from start of line' control sequence (ESC[1K)
fails to handle this corner case correctly and may call
console_clear_xy() with x == width.  That will incorrectly clear the
first char cell of the next line, or in case the cursor happens to be on
the last line overflow the cell buffer by one character (three bytes).

Add a check to the loop to fix that.

Didn't spot any other places with the same problem.  But it's easy to
miss that corner case, so also allocate one extra cell as precaution, so
in case we have simliar issues lurking elsewhere it at least wouldn't be
a buffer overflow.

v2: squashed in additional checks suggested by Christophe de Dinechin.

Reported-by: Alexander Oleinik <alxndr@bu.edu>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Christophe de Dinechin <dinechin@redhat.com>
Message-id: 20190701075301.14165-1-kraxel@redhat.com
ui/console.c