From 40adb336fbeb43c2ae34051174b3a1c13d9e0ff9 Mon Sep 17 00:00:00 2001 From: Aaron Cook Date: Mon, 8 May 2023 09:55:57 -0500 Subject: [PATCH] Fix invalid read errors found with valgrind --- src/cheerios.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/cheerios.c b/src/cheerios.c index 05212e7..1cff23b 100644 --- a/src/cheerios.c +++ b/src/cheerios.c @@ -514,8 +514,17 @@ handle_color(line_buffer_t *lines, int line_idx, int *pos, int apply) return 0; while (p < line_len) { + int fit_col = (p + 7) <= line_len; + int fit_en = (p + 4) <= line_len; + /* fg or bg color */ - if (!memcmp(&line[p], "\e[38;5;", 7) || !memcmp(&line[p], "\e[48;5;", 7)) { + if ( + fit_col && + ( + !memcmp(&line[p], "\e[38;5;", 7) || + !memcmp(&line[p], "\e[48;5;", 7) + ) + ) { char col_str[4] = { 0 }; short *col = line[p + 2] == '3' ? &fg : &bg; @@ -543,13 +552,13 @@ handle_color(line_buffer_t *lines, int line_idx, int *pos, int apply) p++; } /* enable our color */ - else if (!memcmp(&line[p], "\e[1m", 4)) { + else if (fit_en && !memcmp(&line[p], "\e[1m", 4)) { p += 4; enable = 1; break; } /* disable our color */ - else if (!memcmp(&line[p], "\e[0m", 4)) { + else if (fit_en && !memcmp(&line[p], "\e[0m", 4)) { p += 4; enable = 0; break;