From 632e05d8307ebfb256c64aa5c648f3e9f11d7ae9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 29 Jan 2017 23:50:33 +0000 Subject: [PATCH] Fix infinite loop when comparing RankedMatches containing invalid utf8 If we had a word containing some invalid utf8, like a wrong sequence of continuation bytes, we would infinitely loop back to the previous valid character start. Fixes #1157 --- src/ranked_match.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ranked_match.cc b/src/ranked_match.cc index 341a7fbb..ad7e4cec 100644 --- a/src/ranked_match.cc +++ b/src/ranked_match.cc @@ -186,6 +186,7 @@ bool RankedMatch::operator<(const RankedMatch& other) const auto it1 = m_candidate.begin(), it2 = other.m_candidate.begin(); const auto end1 = m_candidate.end(), end2 = other.m_candidate.end(); + auto last1 = it1, last2 = it2; while (true) { // find next mismatch @@ -196,8 +197,8 @@ bool RankedMatch::operator<(const RankedMatch& other) const return it1 == end1 and it2 != end2; // compare codepoints - it1 = utf8::character_start(it1, m_candidate.begin()); - it2 = utf8::character_start(it2, other.m_candidate.begin()); + it1 = utf8::character_start(it1, last1); + it2 = utf8::character_start(it2, last2); const auto cp1 = utf8::read_codepoint(it1, end1); const auto cp2 = utf8::read_codepoint(it2, end2); if (cp1 != cp2) @@ -206,6 +207,7 @@ bool RankedMatch::operator<(const RankedMatch& other) const const bool low2 = iswlower((wchar_t)cp2); return low1 == low2 ? cp1 < cp2 : low1; } + last1 = it1; last2 = it2; } }