Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(script_translator): correction can cause segfault #863

Merged
merged 5 commits into from
Apr 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_);
Copy link
Member

@lotem lotem Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This while-loop is used to drop any correction candidates after the top N (max_corrections_).

If I didn't take it wrong, correction_count_ > max_corrections_ means exactly "correction candidates after the top N" so the loop goes on until the next non-correction candidate is found.

I recently updated this part. I might have broken it. Please blame and review my last change.
One thing that appear weird is the is_correction never set true.

++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
Loading