Skip to content

Commit 906f1ca

Browse files
committed
8292317: Missing null check for Iterator.forEachRemaining implementations
Reviewed-by: sundar, smarks
1 parent 0ec575a commit 906f1ca

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 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
@@ -1714,6 +1714,7 @@ public void remove() {
17141714
throw new UnsupportedOperationException();
17151715
}
17161716
public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) {
1717+
Objects.requireNonNull(action);
17171718
i.forEachRemaining(entryConsumer(action));
17181719
}
17191720
};
@@ -3878,6 +3879,7 @@ public Map.Entry<K,V> next() {
38783879
}
38793880

38803881
public void forEachRemaining(Consumer<? super Entry<K, V>> action) {
3882+
Objects.requireNonNull(action);
38813883
i.forEachRemaining(
38823884
e -> action.accept(checkedEntry(e, valueType)));
38833885
}

test/jdk/java/util/Collections/DelegatingIteratorForEachRemaining.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
23
* Copyright (c) 2018 Google Inc. All rights reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
@@ -195,4 +196,29 @@ private static Map<String, Object> map() {
195196
Class clazz = entrySet.iterator().next().getClass();
196197
assertThrowingIterator(Collections.checkedSet(entrySet, clazz).iterator());
197198
}
199+
200+
/**
201+
* Calls Collections.unmodifiableMap().entrySet().iterator().forEachRemaining() by passing
202+
* that method a {@code null} action and expects that call to fail with a
203+
* {@code NullPointerException}.
204+
*/
205+
@Test
206+
public void testUnmodifiableForEachRemainingNPE() {
207+
final Iterator<?> it = Collections.unmodifiableMap(Map.of()).entrySet().iterator();
208+
// pass null action and expect a NPE
209+
Assert.assertThrows(NullPointerException.class, () -> it.forEachRemaining(null));
210+
}
211+
212+
/**
213+
* Calls Collections.checkedMap().entrySet().iterator().forEachRemaining() by passing
214+
* that method a {@code null} action and expects that call to fail with a
215+
* {@code NullPointerException}.
216+
*/
217+
@Test
218+
public void testCheckedMapForEachRemainingNPE() {
219+
final Iterator<?> it = Collections.checkedMap(Map.of(), String.class,
220+
String.class).entrySet().iterator();
221+
// pass null "action" and expect it to fail with NPE
222+
Assert.assertThrows(NullPointerException.class, () -> it.forEachRemaining(null));
223+
}
198224
}

0 commit comments

Comments
 (0)