From a1c814443264b49a3b8633ebf89071c6ca667a94 Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Sun, 28 Apr 2024 03:11:52 -0400 Subject: [PATCH] fix(context): index out of range in Context::Highlight (#867) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit clamp new_index between 0 and candidate_count - 1 --------- Co-authored-by: 居戎氏 --- src/rime/context.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/rime/context.cc b/src/rime/context.cc index 34ca645231..a63537dd32 100644 --- a/src/rime/context.cc +++ b/src/rime/context.cc @@ -4,6 +4,7 @@ // // 2011-05-08 GONG Chen // +#include #include #include #include @@ -127,12 +128,9 @@ bool Context::Highlight(size_t index) { if (composition_.empty() || !composition_.back().menu) return false; Segment& seg(composition_.back()); - size_t new_index = index; size_t candidate_count = seg.menu->Prepare(index + 1); - if (index >= candidate_count) { - DLOG(INFO) << "selection index exceeds candidate pool, fallback to last"; - new_index = candidate_count - 1; - } + size_t new_index = + candidate_count > 0 ? (std::min)(candidate_count - 1, index) : 0; size_t previous_index = seg.selected_index; if (previous_index == new_index) { DLOG(INFO) << "selection has not changed, currently at " << new_index;