From 78cb71f7b1c088188769ee4dd48f999bf7a4d98e Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:25:34 -0500 Subject: [PATCH] fix: -Wunsafe-buffer-usage warning in HasWordCharacters() (#44171) Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/renderer/api/electron_api_spell_check_client.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/shell/renderer/api/electron_api_spell_check_client.cc b/shell/renderer/api/electron_api_spell_check_client.cc index 2c2e0e5ee636..78c4c955b0f4 100644 --- a/shell/renderer/api/electron_api_spell_check_client.cc +++ b/shell/renderer/api/electron_api_spell_check_client.cc @@ -15,6 +15,7 @@ #include "base/containers/contains.h" #include "base/logging.h" #include "base/numerics/safe_conversions.h" +#include "base/strings/utf_string_conversion_utils.h" #include "base/task/single_thread_task_runner.h" #include "components/spellcheck/renderer/spellcheck_worditerator.h" #include "shell/common/gin_helper/dictionary.h" @@ -29,12 +30,9 @@ namespace electron::api { namespace { -bool HasWordCharacters(const std::u16string& text, int index) { - const char16_t* data = text.data(); - int length = text.length(); - while (index < length) { - uint32_t code = 0; - U16_NEXT(data, index, length, code); +bool HasWordCharacters(const std::u16string& text, size_t index) { + base_icu::UChar32 code; + while (base::ReadUnicodeCharacter(text.c_str(), text.size(), &index, &code)) { UErrorCode error = U_ZERO_ERROR; if (uscript_getScript(code, &error) != USCRIPT_COMMON) return true;