Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/src/main/java/com/typesafe/config/impl/BadMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static <K,V> void store(Entry[] entries, int hash, K k, V v) {
}

private static void store(Entry[] entries, Entry e) {
int i = e.hash % entries.length;
int i = Math.abs(e.hash % entries.length);
Entry old = entries[i]; // old may be null
if (old == null && e.next == null) {
// share the entry since it has no "next"
Expand All @@ -100,8 +100,8 @@ V get(K k) {
if (entries.length == 0) {
return null;
} else {
int hash = Math.abs(k.hashCode());
int i = hash % entries.length;
int hash = k.hashCode();
int i = Math.abs(hash % entries.length);
Entry e = entries[i];
if (e == null)
return null;
Expand Down
14 changes: 14 additions & 0 deletions config/src/test/scala/com/typesafe/config/impl/BadMapTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ class BadMapTest extends TestUtils {
}
}

@Test
def negativeEntryHash(): Unit = {
class Key(value: Int) {
override def hashCode(): Int = value
}

var map = new BadMap[Key, String]()
val negativeHashKey = new Key(Integer.MIN_VALUE)
map = map.copyingPut(negativeHashKey, "value")
.copyingPut(new Key(2), "other value")
.copyingPut(new Key(3), "yet another value")
assertEquals(map.get(negativeHashKey), "value")
}

private class UniqueKeyWithHash(hash: Int) {
override def hashCode(): Int = hash
}
Expand Down
Loading