Skip to content

Commit 4b03e13

Browse files
committed
8285405: add test and check for negative argument to HashMap::newHashMap et al
Reviewed-by: chegar, naoto, lancea, smarks
1 parent 2e0a17c commit 4b03e13

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

src/java.base/share/classes/java/util/HashMap.java

+3
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,9 @@ static int calculateHashMapCapacity(int numMappings) {
25782578
* @since 19
25792579
*/
25802580
public static <K, V> HashMap<K, V> newHashMap(int numMappings) {
2581+
if (numMappings < 0) {
2582+
throw new IllegalArgumentException("Negative number of mappings: " + numMappings);
2583+
}
25812584
return new HashMap<>(calculateHashMapCapacity(numMappings));
25822585
}
25832586

src/java.base/share/classes/java/util/HashSet.java

+3
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ public <T> T[] toArray(T[] a) {
394394
* @since 19
395395
*/
396396
public static <T> HashSet<T> newHashSet(int numElements) {
397+
if (numElements < 0) {
398+
throw new IllegalArgumentException("Negative number of elements: " + numElements);
399+
}
397400
return new HashSet<>(HashMap.calculateHashMapCapacity(numElements));
398401
}
399402

src/java.base/share/classes/java/util/LinkedHashMap.java

+3
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,9 @@ final class LinkedEntryIterator extends LinkedHashIterator
810810
* @since 19
811811
*/
812812
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int numMappings) {
813+
if (numMappings < 0) {
814+
throw new IllegalArgumentException("Negative number of mappings: " + numMappings);
815+
}
813816
return new LinkedHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
814817
}
815818

src/java.base/share/classes/java/util/LinkedHashSet.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -215,6 +215,9 @@ public Spliterator<E> spliterator() {
215215
* @since 19
216216
*/
217217
public static <T> LinkedHashSet<T> newLinkedHashSet(int numElements) {
218+
if (numElements < 0) {
219+
throw new IllegalArgumentException("Negative number of elements: " + numElements);
220+
}
218221
return new LinkedHashSet<>(HashMap.calculateHashMapCapacity(numElements));
219222
}
220223

src/java.base/share/classes/java/util/WeakHashMap.java

+3
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,9 @@ public int characteristics() {
13571357
* @since 19
13581358
*/
13591359
public static <K, V> WeakHashMap<K, V> newWeakHashMap(int numMappings) {
1360+
if (numMappings < 0) {
1361+
throw new IllegalArgumentException("Negative number of mappings: " + numMappings);
1362+
}
13601363
return new WeakHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
13611364
}
13621365

test/jdk/java/util/HashMap/WhiteBoxResizeTest.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.lang.invoke.MethodHandles;
3030
import java.lang.invoke.MethodType;
3131
import java.lang.invoke.VarHandle;
32-
import java.lang.reflect.Field;
3332
import java.util.AbstractMap;
3433
import java.util.AbstractSet;
3534
import java.util.ArrayList;
@@ -44,10 +43,12 @@
4443
import java.util.Set;
4544
import java.util.WeakHashMap;
4645
import java.util.function.Consumer;
46+
import java.util.function.IntFunction;
4747
import java.util.function.Supplier;
4848

4949
import static org.testng.Assert.assertEquals;
5050
import static org.testng.Assert.assertNull;
51+
import static org.testng.Assert.assertThrows;
5152

5253
/*
5354
* @test
@@ -426,4 +427,27 @@ public int capacity() {
426427
}
427428
}
428429

430+
@DataProvider(name = "negativeNumMappings")
431+
public Iterator<Object[]> negativeNumMappings() {
432+
final List<Object[]> methods = new ArrayList<>();
433+
methods.add(new Object[] {(IntFunction<?>) HashMap::newHashMap, "HashMap::newHashMap"});
434+
methods.add(new Object[] {(IntFunction<?>) LinkedHashMap::newLinkedHashMap,
435+
"LinkedHashMap::newLinkedHashMap"});
436+
methods.add(new Object[] {(IntFunction<?>) WeakHashMap::newWeakHashMap,
437+
"WeakHashMap::newWeakHashMap"});
438+
methods.add(new Object[] {(IntFunction<?>) HashSet::newHashSet, "HashSet::newHashSet"});
439+
methods.add(new Object[] {(IntFunction<?>) LinkedHashSet::newLinkedHashSet,
440+
"LinkedHashSet::newLinkedHashSet"});
441+
return methods.iterator();
442+
}
443+
444+
/**
445+
* Tests that the APIs that take {@code numMappings} or {@code numElements} as a parameter for
446+
* creating the collection instance (for example: {@link HashMap#newHashMap(int)}), throw
447+
* an {@code IllegalArgumentException} when a negative value is passed to them
448+
*/
449+
@Test(dataProvider = "negativeNumMappings")
450+
public void testNegativeNumMappings(final IntFunction<?> method, final String methodName) {
451+
assertThrows(IllegalArgumentException.class, () -> method.apply(-1));
452+
}
429453
}

0 commit comments

Comments
 (0)