From 0457c334181a62c192c6f1ae03eea0d75f424e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 4 Jul 2019 09:57:02 +0200 Subject: [PATCH] csi: fix parsing of 256-color and 24-bit color SGRs There may be more SGR sequences after the 256-color/24-bit sequence. Thus, check that we have *enough* parameters to parse the 256-color/24-bit SGR. It doesn't have to be *exactly* the required number of parameters though. Fixes issues with sequences like: \e[38;2;1;48;2;1m --- csi.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/csi.c b/csi.c index 4b545d31..823b010b 100644 --- a/csi.c +++ b/csi.c @@ -139,14 +139,14 @@ csi_sgr(struct terminal *term) break; case 38: { - if (term->vt.params.idx - i - 1 == 2 && + if (term->vt.params.idx - i - 1 >= 2 && term->vt.params.v[i + 1].value == 5) { size_t idx = term->vt.params.v[i + 2].value; term->vt.attrs.foreground = colors256[idx]; term->vt.attrs.have_foreground = true; i += 2; - } else if (term->vt.params.idx - i - 1 == 4 && + } else if (term->vt.params.idx - i - 1 >= 4 && term->vt.params.v[i + 1].value == 2) { uint8_t r = term->vt.params.v[i + 2].value; @@ -185,14 +185,14 @@ csi_sgr(struct terminal *term) break; case 48: { - if (term->vt.params.idx - i - 1 == 2 && + if (term->vt.params.idx - i - 1 >= 2 && term->vt.params.v[i + 1].value == 5) { size_t idx = term->vt.params.v[i + 2].value; term->vt.attrs.background = colors256[idx]; term->vt.attrs.have_background = true; i += 2; - } else if (term->vt.params.idx - i - 1 == 4 && + } else if (term->vt.params.idx - i - 1 >= 4 && term->vt.params.v[i + 1].value == 2) { uint8_t r = term->vt.params.v[i + 2].value;