From 550d0f915bb8a489d86501eae48d8b47b44050d1 Mon Sep 17 00:00:00 2001 From: Evgeny Pestov Date: Mon, 14 Feb 2022 18:33:41 +0700 Subject: [PATCH] Use std::out_of_range in Utf8CharSizeAt --- src/common/stringutils.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp index a7d30f6a..626c5c39 100644 --- a/src/common/stringutils.cpp +++ b/src/common/stringutils.cpp @@ -156,11 +156,17 @@ std::wstring StrUtils::Utf8StringToUnicode(const std::string &str) { std::wstring result; unsigned int pos = 0; + int len; while (pos < str.size()) { - int len = StrUtils::Utf8CharSizeAt(str, pos); - if (len == 0) + try + { + len = StrUtils::Utf8CharSizeAt(str, pos); + } + catch (std::out_of_range &e) + { break; + } std::string ch = str.substr(pos, len); result += static_cast(StrUtils::Utf8CharToUnicode(ch)); @@ -172,7 +178,7 @@ std::wstring StrUtils::Utf8StringToUnicode(const std::string &str) int StrUtils::Utf8CharSizeAt(const std::string &str, unsigned int pos) { if (pos >= str.size()) - return 0; + throw std::out_of_range("Index is greater than size"); const char c = str[pos]; if((c & 0b1000'0000) == 0b0000'0000)