Skip to content

Commit

Permalink
8292317: Missing null check for Iterator.forEachRemaining implementat…
Browse files Browse the repository at this point in the history
…ions

Reviewed-by: sundar, smarks
  • Loading branch information
jaikiran committed Nov 19, 2022
1 parent 0ec575a commit 906f1ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/java.base/share/classes/java/util/Collections.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -1714,6 +1714,7 @@ public void remove() {
throw new UnsupportedOperationException();
}
public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) {
Objects.requireNonNull(action);
i.forEachRemaining(entryConsumer(action));
}
};
Expand Down Expand Up @@ -3878,6 +3879,7 @@ public Map.Entry<K,V> next() {
}

public void forEachRemaining(Consumer<? super Entry<K, V>> action) {
Objects.requireNonNull(action);
i.forEachRemaining(
e -> action.accept(checkedEntry(e, valueType)));
}
Expand Down
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Google Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -195,4 +196,29 @@ private static Map<String, Object> map() {
Class clazz = entrySet.iterator().next().getClass();
assertThrowingIterator(Collections.checkedSet(entrySet, clazz).iterator());
}

/**
* Calls Collections.unmodifiableMap().entrySet().iterator().forEachRemaining() by passing
* that method a {@code null} action and expects that call to fail with a
* {@code NullPointerException}.
*/
@Test
public void testUnmodifiableForEachRemainingNPE() {
final Iterator<?> it = Collections.unmodifiableMap(Map.of()).entrySet().iterator();
// pass null action and expect a NPE
Assert.assertThrows(NullPointerException.class, () -> it.forEachRemaining(null));
}

/**
* Calls Collections.checkedMap().entrySet().iterator().forEachRemaining() by passing
* that method a {@code null} action and expects that call to fail with a
* {@code NullPointerException}.
*/
@Test
public void testCheckedMapForEachRemainingNPE() {
final Iterator<?> it = Collections.checkedMap(Map.of(), String.class,
String.class).entrySet().iterator();
// pass null "action" and expect it to fail with NPE
Assert.assertThrows(NullPointerException.class, () -> it.forEachRemaining(null));
}
}

1 comment on commit 906f1ca

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.