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
diff --git a/libdexfile/dex/utf.cc b/libdexfile/dex/utf.cc
index d09da73..ed07568 100644
--- a/libdexfile/dex/utf.cc
+++ b/libdexfile/dex/utf.cc
@@ -194,9 +194,10 @@
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 d2f22d1..c7a6a34 100644
--- a/libdexfile/dex/utf_test.cc
+++ b/libdexfile/dex/utf_test.cc
@@ -378,4 +378,11 @@
}
}
+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