Skip to content

Commit 86b31e7

Browse files
committed
Improve error message for hash collisions, tune tertiary slot sizes
1 parent 3357e8f commit 86b31e7

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/main/java/com/fasterxml/jackson/core/sym/ByteQuadsCanonicalizer.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1016,9 +1016,9 @@ public int calcHash(int q1, int q2, int q3)
10161016
{ // use same algorithm as multi-byte, tested to work well
10171017
int hash = q1 ^ _seed;
10181018
hash += (hash >>> 9);
1019-
hash *= MULT;
1019+
hash *= MULT3;
10201020
hash += q2;
1021-
hash *= MULT2;
1021+
hash *= MULT;
10221022
hash += (hash >>> 15);
10231023
hash ^= q3;
10241024
// 26-Mar-2015, tatu: As per two-quad case, a short shift seems to help more here
@@ -1183,13 +1183,14 @@ private final int _spilloverStart() {
11831183

11841184
protected void reportTooManyCollisions()
11851185
{
1186-
// First: do not fuzz about small symbol tables
1187-
if (_hashSize <= 512) { // would have spill-over area of 64 entries
1186+
// First: do not fuzz about small symbol tables; may get balanced by doubling up
1187+
if (_hashSize <= 1024) { // would have spill-over area of 128 entries
11881188
return;
11891189
}
11901190
throw new IllegalStateException("Spill-over slots in symbol table with "+_count
11911191
+" entries, hash area of "+_hashSize+" slots is now full (all "
1192-
+(_hashSize >> 3)+" slots -- suspect a DoS attack based on hash collisions");
1192+
+(_hashSize >> 3)+" slots -- suspect a DoS attack based on hash collisions."
1193+
+" You can disable the check via `JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW`");
11931194
}
11941195

11951196
static int _calcTertiaryShift(int primarySlots)
@@ -1204,7 +1205,7 @@ static int _calcTertiaryShift(int primarySlots)
12041205
if (tertSlots <= 256) { // buckets of 8 slots (up to 256 == 32 x 8)
12051206
return 5;
12061207
}
1207-
if (tertSlots <= 2048) { // buckets of 16 slots (up to 1024 == 64 x 16)
1208+
if (tertSlots <= 1024) { // buckets of 16 slots (up to 1024 == 64 x 16)
12081209
return 6;
12091210
}
12101211
// and biggest buckets have 32 slots

src/test/java/com/fasterxml/jackson/core/sym/TestSymbolTables.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ public void testSyntheticWithBytesNew() throws IOException
7979

8080
// fragile, but essential to verify low collision counts;
8181
// anywhere between 70-80% primary matches
82-
assertEquals(8515, symbols.primaryCount());
82+
assertEquals(8524, symbols.primaryCount());
8383
// secondary between 10-20%
84-
assertEquals(2525, symbols.secondaryCount());
84+
assertEquals(2534, symbols.secondaryCount());
8585
// and most of remaining in tertiary
86-
assertEquals(960, symbols.tertiaryCount());
86+
assertEquals(942, symbols.tertiaryCount());
8787
// so that spill-over is empty or close to
8888
assertEquals(0, symbols.spilloverCount());
8989
}

src/test/java/com/fasterxml/jackson/core/sym/TestSymbolsWithMediaItem.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.core.sym;
22

33
import java.io.IOException;
4-
import java.nio.charset.Charset;
54

65
import com.fasterxml.jackson.core.*;
76

0 commit comments

Comments
 (0)