From 4dfcd4cb4e80e3ecef229b66c28eb530101e19e3 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Fri, 26 Aug 2022 15:02:05 +0200 Subject: [PATCH] reader: Check bounds for color This fixes a crash when you open the history pager and then do history-token-search-backward (e.g. alt+. or alt-up). It would sometimes crash because the `colors.at(i)` was an out-of-bounds access. Note: This might still leave the highlighting offset in some cases (not quite sure why), but at least it doesn't *crash*, and the search generally *works*. --- src/reader.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/reader.cpp b/src/reader.cpp index 0651ec089..a6ecf848f 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1169,7 +1169,13 @@ void reader_data_t::paint_layout(const wchar_t *reason) { // Highlight any history search. if (!conf.in_silent_mode && data.history_search_range) { - for (size_t i = data.history_search_range->start; i < data.history_search_range->end(); + // std::min gets confused about types here. + size_t end = data.history_search_range->end(); + if (colors.size() < end) { + end = colors.size(); + } + + for (size_t i = data.history_search_range->start; i < end; i++) { colors.at(i).background = highlight_role_t::search_match; } @@ -1178,7 +1184,8 @@ void reader_data_t::paint_layout(const wchar_t *reason) { // Apply any selection. if (data.selection.has_value()) { highlight_spec_t selection_color = {highlight_role_t::normal, highlight_role_t::selection}; - for (size_t i = data.selection->start; i < std::min(selection->stop, colors.size()); i++) { + auto end = std::min(selection->stop, colors.size()); + for (size_t i = data.selection->start; i < end; i++) { colors.at(i) = selection_color; } }