diff options
author | 2018-12-10 12:14:36 +0000 | |
---|---|---|
committer | 2018-12-10 15:14:40 +0000 | |
commit | ca0f2dceb2ab4fdbebf9d2b75c774c363b4191b4 (patch) | |
tree | a2d6822b068e29c2bab2edc643dd208206e2e77b | |
parent | b023b3fd99a3369479e965c8e5d1d79b2491f75d (diff) |
Fix ComputeModifiedUtf8Hash().
Explicitly cast the character to uint8_t so that we do not
produce different results depending on whether the platform
has a signed or unsigned char.
Test: Additional test in utf_test.
Bug: 120749178
Change-Id: Icf329baa9eb95c2db820b51873e72d95aabf398e
-rw-r--r-- | libdexfile/dex/utf.cc | 5 | ||||
-rw-r--r-- | libdexfile/dex/utf_test.cc | 7 |
2 files changed, 10 insertions, 2 deletions
diff --git a/libdexfile/dex/utf.cc b/libdexfile/dex/utf.cc index d09da735f2..ed07568fbc 100644 --- a/libdexfile/dex/utf.cc +++ b/libdexfile/dex/utf.cc @@ -194,9 +194,10 @@ int32_t ComputeUtf16HashFromModifiedUtf8(const char* utf8, size_t utf16_length) uint32_t ComputeModifiedUtf8Hash(const char* chars) { uint32_t hash = 0; while (*chars != '\0') { - hash = hash * 31 + *chars++; + hash = hash * 31 + static_cast<uint8_t>(*chars); + ++chars; } - return static_cast<int32_t>(hash); + return hash; } int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8, const uint16_t* utf16, diff --git a/libdexfile/dex/utf_test.cc b/libdexfile/dex/utf_test.cc index d2f22d16ef..c7a6a342ff 100644 --- a/libdexfile/dex/utf_test.cc +++ b/libdexfile/dex/utf_test.cc @@ -378,4 +378,11 @@ TEST_F(UtfTest, ExhaustiveBidirectionalCodePointCheck) { } } +TEST_F(UtfTest, NonAscii) { + const char kNonAsciiCharacter = '\x80'; + const char input[] = { kNonAsciiCharacter, '\0' }; + uint32_t hash = ComputeModifiedUtf8Hash(input); + EXPECT_EQ(static_cast<uint8_t>(kNonAsciiCharacter), hash); +} + } // namespace art |