From 86ef158c00f5ed576bf0c73bf197ffad95aece0f Mon Sep 17 00:00:00 2001 From: suve Date: Mon, 7 Oct 2019 17:09:15 +0200 Subject: [PATCH] Detect invalid values in StringUtils::Utf8CharSizeAt() --- src/common/stringutils.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp index b8639df3..fd8387f5 100644 --- a/src/common/stringutils.cpp +++ b/src/common/stringutils.cpp @@ -173,14 +173,19 @@ int StrUtils::Utf8CharSizeAt(const std::string &str, unsigned int pos) if (pos >= str.size()) return 0; - if ((str[pos] & 0x80) == 0) - return 1; - else if ((str[pos] & 0xC0) == 0xC0) - return 2; - else + const char c = str[pos]; + if(c >= 0xF0) + return 4; + if(c >= 0xE0) return 3; + if(c >= 0xC0) + return 2; - return 0; + // Invalid char - unexpected continuation byte + if(c >= 0x80) + return 0; + + return 1; } std::size_t StrUtils::Utf8StringLength(const std::string &str)