8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so)
authorsgehwolf
Tue, 15 May 2018 15:36:46 +0200
changeset 50125 2d9dd2b876a0
parent 50124 781f36c0831e
child 50126 98f57dff16f3
8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so) Summary: Perform hash operation on local unsigned type. Reviewed-by: shade, dholmes, alanb
src/java.base/share/native/libjimage/imageFile.cpp
--- a/src/java.base/share/native/libjimage/imageFile.cpp	Wed May 16 13:14:58 2018 +0200
+++ b/src/java.base/share/native/libjimage/imageFile.cpp	Tue May 15 15:36:46 2018 +0200
@@ -57,14 +57,16 @@
 
 // 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.