Skip to content

Commit

Permalink
fix(context): index out of range in Context::Highlight (#867)
Browse files Browse the repository at this point in the history
clamp new_index between 0 and candidate_count - 1

---------

Co-authored-by: 居戎氏 <chen.sst@gmail.com>
  • Loading branch information
LEOYoon-Tsaw and lotem authored Apr 28, 2024
1 parent 1c9c71d commit a1c8144
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/rime/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// 2011-05-08 GONG Chen <chen.sst@gmail.com>
//
#include <algorithm>
#include <utility>
#include <rime/candidate.h>
#include <rime/context.h>
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a1c8144

Please sign in to comment.