Skip to contentSkip to conversation

TextConsole: saturate escape parameter in TTY_STATE_CSI

PATCH Applied Laszlo Ersek 3 participants 3 messages Sep 17, 2012 – Sep 22, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 TextConsole: saturate escape parameter in TTY_STATE_CSI

---
Build tested.

 console.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
console.c +5 -2
index c1ed5e0..67080f4 100644
@@ -938,8 +938,11 @@ static void console_putchar(TextConsole *s, int ch)
     case TTY_STATE_CSI: /* handle escape sequence parameters */
         if (ch >= '0' && ch <= '9') {
             if (s->nb_esc_params < MAX_ESC_PARAMS) {
-                s->esc_params[s->nb_esc_params] =
-                    s->esc_params[s->nb_esc_params] * 10 + ch - '0';
+                int *param = &s->esc_params[s->nb_esc_params];
+                int digit = (ch - '0');
+
+                *param = (*param <= (INT_MAX - digit) / 10) ?
+                         *param * 10 + digit : INT_MAX;
             }
         } else {
             if (s->nb_esc_params < MAX_ESC_PARAMS)
Signature · 1 line
1.7.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Laszlo Ersek

---
Build tested.

 console.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
console.c +0 -0
index c1ed5e0..67080f4 100644
@@ -938,8 +938,11 @@ static void console_putchar(TextConsole *s, int ch)
     case TTY_STATE_CSI: /* handle escape sequence parameters */
         if (ch >= '0' && ch <= '9') {
             if (s->nb_esc_params < MAX_ESC_PARAMS) {

console.c static void console_putchar(TextConsole *s, int ch) · line 948

Quoted 10 lines
console.c +5 -2
-                s->esc_params[s->nb_esc_params] =
-                    s->esc_params[s->nb_esc_params] * 10 + ch - '0';
+                int *param = &s->esc_params[s->nb_esc_params];
+                int digit = (ch - '0');
+
+                *param = (*param <= (INT_MAX - digit) / 10) ?
+                         *param * 10 + digit : INT_MAX;
             }
         } else {
             if (s->nb_esc_params < MAX_ESC_PARAMS)

Before this patch, silent integer overflow. Exact behavior depends on hosts int type. For instance, \e[4294967296 is the same as \e[0 with 32 bit int, but with 64 bit int.

What does a real vt100 do? I don't have one anymore. For what it's worth, both xterm and Xfce Terminal appear to saturate at some "big" number ("big" compared to the argument values that are actually useful; INT_MAX should do fine). In particular, \e[4294967296 does *not* behave like \e[0.

Therefore, changing QEMU to saturate makes sense.