From d17f0e96c54360bd513939a6c6a13789de30a04d Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 18 Apr 2024 20:47:32 +0800 Subject: [PATCH 1/5] fix(script_translator): correction can cause segfault Previously, the correction count limit doesn't really work, and it will take all candidates. When finding the candidates for the last page, ScriptTranslation::PrepareCandidate can return nullptr, leading to segfault. --- src/rime/gear/script_translator.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rime/gear/script_translator.cc b/src/rime/gear/script_translator.cc index 853b8fc33c..40f4280f7d 100644 --- a/src/rime/gear/script_translator.cc +++ b/src/rime/gear/script_translator.cc @@ -431,7 +431,7 @@ bool ScriptTranslation::Next() { } while (enable_correction_ && syllabifier_->IsCandidateCorrection(*candidate_) && // limit the number of correction candidates - ++correction_count_ > max_corrections_); + ++correction_count_ < max_corrections_); ++candidate_index_; return !CheckEmpty(); } @@ -528,6 +528,8 @@ bool ScriptTranslation::PrepareCandidate() { candidate_->set_quality(std::exp(entry->weight) + translator_->initial_quality() + (IsNormalSpelling() ? 0 : -1)); + } else { + return false; } return true; } From 1feb30fec8fa6dda9a9c3509055875e65dd42314 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 18 Apr 2024 20:59:43 +0800 Subject: [PATCH 2/5] chore: clang-format --- src/rime/algo/syllabifier.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rime/algo/syllabifier.h b/src/rime/algo/syllabifier.h index 1d46682c01..157e03f29c 100644 --- a/src/rime/algo/syllabifier.h +++ b/src/rime/algo/syllabifier.h @@ -20,7 +20,7 @@ class Corrector; using SyllableId = int32_t; struct EdgeProperties : SpellingProperties { - EdgeProperties(SpellingProperties sup) : SpellingProperties(sup){}; + EdgeProperties(SpellingProperties sup) : SpellingProperties(sup) {}; EdgeProperties() = default; bool is_correction = false; }; From 350693db808c6854639725711c699730f152f168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Sun, 21 Apr 2024 23:51:31 +0800 Subject: [PATCH 3/5] Revert "chore: clang-format" This reverts commit 1feb30fec8fa6dda9a9c3509055875e65dd42314. --- src/rime/algo/syllabifier.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rime/algo/syllabifier.h b/src/rime/algo/syllabifier.h index 157e03f29c..1d46682c01 100644 --- a/src/rime/algo/syllabifier.h +++ b/src/rime/algo/syllabifier.h @@ -20,7 +20,7 @@ class Corrector; using SyllableId = int32_t; struct EdgeProperties : SpellingProperties { - EdgeProperties(SpellingProperties sup) : SpellingProperties(sup) {}; + EdgeProperties(SpellingProperties sup) : SpellingProperties(sup){}; EdgeProperties() = default; bool is_correction = false; }; From 3d7fac80c9127d90bb646afb5954921748f2e934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Sun, 21 Apr 2024 23:55:48 +0800 Subject: [PATCH 4/5] revert: ++correction_count_ > max_corrections_ it is the correct logic to discard more correction candidates after the top N; if reverted, all correction candidates except the top N are displayed. --- src/rime/gear/script_translator.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rime/gear/script_translator.cc b/src/rime/gear/script_translator.cc index 40f4280f7d..389851e243 100644 --- a/src/rime/gear/script_translator.cc +++ b/src/rime/gear/script_translator.cc @@ -431,7 +431,7 @@ bool ScriptTranslation::Next() { } while (enable_correction_ && syllabifier_->IsCandidateCorrection(*candidate_) && // limit the number of correction candidates - ++correction_count_ < max_corrections_); + ++correction_count_ > max_corrections_); ++candidate_index_; return !CheckEmpty(); } From 7ffaeb8396a17d6a053f3d2e74882b24019c268c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Sun, 21 Apr 2024 23:58:03 +0800 Subject: [PATCH 5/5] fix: correctly update member variables for the no more candidates to display case --- src/rime/gear/script_translator.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/rime/gear/script_translator.cc b/src/rime/gear/script_translator.cc index 389851e243..028b659e58 100644 --- a/src/rime/gear/script_translator.cc +++ b/src/rime/gear/script_translator.cc @@ -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) { @@ -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 { @@ -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(); @@ -528,10 +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() {