8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so)

Perform hash operation on local unsigned type.

Reviewed-by: shade, dholmes, alanb
This commit is contained in:
Severin Gehwolf 2018-05-15 15:36:46 +02:00
parent 5ee4a26ecb
commit 2bd15814dc

View file

@ -57,14 +57,16 @@ const char FileSeparator = '/';
// Compute the Perfect Hashing hash code for the supplied UTF-8 string.
s4 ImageStrings::hash_code(const char* string, s4 seed) {
assert(seed > 0 && "invariant");
// Access bytes as unsigned.
u1* bytes = (u1*)string;
u4 useed = (u4)seed;
// Compute hash code.
for (u1 byte = *bytes++; byte; byte = *bytes++) {
seed = (seed * HASH_MULTIPLIER) ^ byte;
useed = (useed * HASH_MULTIPLIER) ^ byte;
}
// Ensure the result is not signed.
return seed & 0x7FFFFFFF;
return (s4)(useed & 0x7FFFFFFF);
}
// Match up a string in a perfect hash table.