Browse Source

Fix offset handling in Murmur3Hasher (#133193)

Felix Barnsteiner 1 month ago
parent
commit
984dea7929

+ 5 - 0
docs/changelog/133193.yaml

@@ -0,0 +1,5 @@
+pr: 133193
+summary: Fix offset handling in Murmur3Hasher
+area: Infra/Core
+type: bug
+issues: []

+ 1 - 1
server/src/main/java/org/elasticsearch/common/hash/Murmur3Hasher.java

@@ -75,7 +75,7 @@ public class Murmur3Hasher {
                 System.arraycopy(inputBytes, offset + numBytesToHash, remainder, 0, remainderLength);
             }
         } else {
-            System.arraycopy(inputBytes, 0, remainder, remainderLength, length);
+            System.arraycopy(inputBytes, offset, remainder, remainderLength, length);
             remainderLength += length;
         }
     }

+ 5 - 1
server/src/test/java/org/elasticsearch/common/hashing/Murmur3HasherTests.java

@@ -35,8 +35,12 @@ public class Murmur3HasherTests extends ESTestCase {
         expected.h2 = upper;
 
         byte[] bytes = inputString.getBytes(StandardCharsets.UTF_8);
+        int padding = randomInt(8);
+        int offset = randomInt(padding);
+        byte[] paddedBytes = new byte[bytes.length + padding];
+        System.arraycopy(bytes, 0, paddedBytes, offset, bytes.length);
         Murmur3Hasher mh = new Murmur3Hasher(seed);
-        mh.update(bytes);
+        mh.update(paddedBytes, offset, bytes.length);
         MurmurHash3.Hash128 actual = mh.digestHash();
         assertHash(expected, actual);
     }