From afadb98d9dca5e7eb8aae057195fcfafb543f8da Mon Sep 17 00:00:00 2001 From: Roger Kapsi Date: Thu, 16 Apr 2015 18:27:11 -0400 Subject: [PATCH] Fix for ByteString#hashCode() Motivation: ByteString#hashCode() trashes its own hash code if it's being accessed concurrently Modifications: Pull the ByteString#hash into a local variable and calculate it locally. Result: ByteString#hashCode() is no longer returning a junk value. --- .../src/main/java/io/netty/util/ByteString.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/io/netty/util/ByteString.java b/common/src/main/java/io/netty/util/ByteString.java index 258dcca610..6a2250ceec 100644 --- a/common/src/main/java/io/netty/util/ByteString.java +++ b/common/src/main/java/io/netty/util/ByteString.java @@ -393,15 +393,15 @@ public class ByteString { @Override public int hashCode() { - if (hash != 0) { - return hash; + int h = hash; + if (h == 0) { + for (int i = 0; i < value.length; ++i) { + h = h * HASH_CODE_PRIME ^ value[i] & HASH_CODE_PRIME; + } + + hash = h; } - - for (int i = 0; i < value.length; ++i) { - hash = hash * HASH_CODE_PRIME ^ value[i] & HASH_CODE_PRIME; - } - - return hash; + return h; } /**