From e09976042d3e85a77a39cdcafe16f5e2834df19f Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sun, 10 May 2026 19:25:07 +0100 Subject: [PATCH] Reduce the scope of the first keyword_index (cppcheck recommendation), and in the formatter, guard against a size_t underflow by checking the write position each time it's used (implied by cppcheck). --- src/pv/format/sgr.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/pv/format/sgr.c b/src/pv/format/sgr.c index 7b2e01b..9767c22 100644 --- a/src/pv/format/sgr.c +++ b/src/pv/format/sgr.c @@ -72,10 +72,10 @@ struct sgr_keyword_map_s { { "bg-default", 0, 49 }, { NULL, 0, 0 } }; - int keyword_index; /* Calculate the lengths of each keyword on the first call. */ if (0 == keywords[0].bytes) { + int keyword_index; for (keyword_index = 0; NULL != keywords[keyword_index].keyword; keyword_index++) { keywords[keyword_index].bytes = strlen(keywords[keyword_index].keyword); /* flawfinder: ignore */ /* flawfinder - strlen() on a static null-terminated string is OK. */ @@ -99,6 +99,7 @@ pvdisplay_bytecount_t pv_formatter_sgr(pvformatter_args_t args) { /*@keep@ */ static struct sgr_keyword_map_s *keywords; char content[1024]; /* flawfinder: ignore */ + size_t content_buffer_size = sizeof(content); pvdisplay_bytecount_t write_position, read_position, keyword_start, keyword_length; int numeric_value, code_count, most_recent_code; @@ -175,23 +176,26 @@ pvdisplay_bytecount_t pv_formatter_sgr(pvformatter_args_t args) debug("code=%d", code); if (code >= 0) { - if (code_count > 15) { + if (code_count > 15 && write_position < content_buffer_size) { write_position += pv_snprintf(content + write_position, sizeof(content) - write_position, "%s", "m"); code_count = 0; } - if (0 == code_count) { + if (0 == code_count && write_position < content_buffer_size) { write_position += pv_snprintf(content + write_position, sizeof(content) - write_position, "%s", "\033["); - } else { + } else if (write_position < content_buffer_size) { write_position += pv_snprintf(content + write_position, sizeof(content) - write_position, "%s", ";"); } - write_position += - pv_snprintf(content + write_position, sizeof(content) - write_position, "%d", code); + if (write_position < content_buffer_size) { + write_position += + pv_snprintf(content + write_position, sizeof(content) - write_position, + "%d", code); + } code_count++; most_recent_code = code; } @@ -201,7 +205,7 @@ pvdisplay_bytecount_t pv_formatter_sgr(pvformatter_args_t args) } } - if (code_count > 0) + if (code_count > 0 && write_position < content_buffer_size) write_position += pv_snprintf(content + write_position, sizeof(content) - write_position, "%s", "m"); if (most_recent_code > 0) {