changeset 50674:2d9dd2b876a0

8203223: Signed integer overflow in ImageStrings::hash_code (libjimage.so) Summary: Perform hash operation on local unsigned type. Reviewed-by: shade, dholmes, alanb
author sgehwolf
date Tue, 15 May 2018 15:36:46 +0200
parents 781f36c0831e
children 98f57dff16f3
files src/java.base/share/native/libjimage/imageFile.cpp
diffstat 1 files changed, 4 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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.