From c954f9efb33d38f55fa416103e7f65dc5cf2bf0b Mon Sep 17 00:00:00 2001 From: Mikhail Aksenov Date: Wed, 24 Aug 2022 15:48:45 +0300 Subject: [PATCH] Use length to compute hash of Utf8 string Signed-off-by: Mikhail Aksenov --- runtime/ecma_string.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/ecma_string.cpp b/runtime/ecma_string.cpp index 8b6d085c3..d62248bc5 100644 --- a/runtime/ecma_string.cpp +++ b/runtime/ecma_string.cpp @@ -375,13 +375,13 @@ bool EcmaString::StringCopy(Span &dst, size_t dstMax, Span &src, siz return true; } -static int32_t ComputeHashForUtf8(const uint8_t *utf8Data) +static int32_t ComputeHashForUtf8(const uint8_t *utf8Data, size_t utf8Len) { if (utf8Data == nullptr) { return 0; } uint32_t hash = 0; - while (*utf8Data != '\0') { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + while (utf8Len-- > 0) { constexpr size_t SHIFT = 5; hash = (hash << SHIFT) - hash + *utf8Data++; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) } @@ -409,7 +409,7 @@ uint32_t EcmaString::ComputeHashcodeUtf8(const uint8_t *utf8Data, size_t utf8Len { uint32_t hash; if (canBeCompress) { - hash = ComputeHashForUtf8(utf8Data); + hash = ComputeHashForUtf8(utf8Data, utf8Len); } else { auto utf16Len = base::utf_helper::Utf8ToUtf16Size(utf8Data, utf8Len); PandaVector tmpBuffer(utf16Len); -- Gitee