Skip to content

Commit

Permalink
fix(script_translator): correction can cause segfault (#863)
Browse files Browse the repository at this point in the history
* fix(script_translator): correction can cause segfault

When finding the candidates for the last page,
ScriptTranslation::PrepareCandidate can return nullptr, leading
to segfault.

    ++correction_count_ > max_corrections_

it is the correct logic to discard more correction candidates after the top N;
if inverted, all correction candidates except the top N are displayed.

correctly update member variables for the no more candidates to display case.

---------

Co-authored-by: 居戎氏 <chen.sst@gmail.com>
  • Loading branch information
ksqsf and lotem committed Apr 21, 2024
1 parent 0533fbc commit feec253
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,7 @@ bool ScriptTranslation::Evaluate(Dictionary* dict, UserDictionary* user_dict) {
}

bool ScriptTranslation::Next() {
bool is_correction;
do {
is_correction = false;
if (exhausted())
return false;
if (candidate_source_ == kUninitialized) {
Expand Down Expand Up @@ -432,8 +430,11 @@ bool ScriptTranslation::Next() {
syllabifier_->IsCandidateCorrection(*candidate_) &&
// limit the number of correction candidates
++correction_count_ > max_corrections_);
++candidate_index_;
return !CheckEmpty();
if (!CheckEmpty()) {
++candidate_index_;
return true;
}
return false;
}

bool ScriptTranslation::IsNormalSpelling() const {
Expand Down Expand Up @@ -515,6 +516,7 @@ bool ScriptTranslation::PrepareCandidate() {
candidate_->set_quality(std::exp(entry->weight) +
translator_->initial_quality() +
(IsNormalSpelling() ? 0.5 : -0.5));
return true;
} else if (phrase_code_length > 0) {
DictEntryIterator& iter = phrase_iter_->second;
const auto& entry = iter.Peek();
Expand All @@ -528,8 +530,12 @@ bool ScriptTranslation::PrepareCandidate() {
candidate_->set_quality(std::exp(entry->weight) +
translator_->initial_quality() +
(IsNormalSpelling() ? 0 : -1));
return true;
} else {
candidate_source_ = kUninitialized;
candidate_ = nullptr;
return false;
}
return true;
}

bool ScriptTranslation::CheckEmpty() {
Expand Down

0 comments on commit feec253

Please sign in to comment.