Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8292317: Missing null check for Iterator.forEachRemaining implementations #11154

Closed
wants to merge 2 commits into from

Conversation

jaikiran
Copy link
Member

@jaikiran jaikiran commented Nov 15, 2022

Can I please get a review of this change which proposes to fix https://bugs.openjdk.org/browse/JDK-8292317?

The java.util.Iterator has a forEachRemaining(Consumer<? super E> action) method. As per its contract, the implementations are expected to throw a NullPointerException if the passed action is null.

java.util.Collections has a couple of places where it wraps the passed action into another and passes that wrapped action to the underlying java.util.Iterator's default forEachRemaining implementation which is:

Objects.requireNonNull(action);
  while (hasNext())
      action.accept(next());

Since the passed action is now a non-null wrapper, the implementation goes ahead and advances the iterator to the next entry and invokes on the non-null action to carry out its action. That non-null wrapper action then calls the null user passed action and runs into an expected NullPointerException. However, at this point the iterator has already advanced and that is what the bug is.

The commit in this PR introduces a trivial null check on the action very early in the call even before wrapping such an action. This prevents any further logic to execute if action is null.

New test methods have been added to the existing test class test/jdk/java/util/Collections/DelegatingIteratorForEachRemaining.java. This test class was introduced in https://bugs.openjdk.org/browse/JDK-8205184 where this delegation logic was added for the forEachRemaining methods. These new test methods reproduce the failure and verify the fix.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8292317: Missing null check for Iterator.forEachRemaining implementations

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/11154/head:pull/11154
$ git checkout pull/11154

Update a local copy of the PR:
$ git checkout pull/11154
$ git pull https://git.openjdk.org/jdk pull/11154/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11154

View PR using the GUI difftool:
$ git pr show -t 11154

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/11154.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 15, 2022

👋 Welcome back jpai! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 15, 2022
@openjdk
Copy link

openjdk bot commented Nov 15, 2022

@jaikiran The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Nov 15, 2022
@mlbridge
Copy link

mlbridge bot commented Nov 15, 2022

Webrevs

Copy link
Member

@sundararajana sundararajana left a comment

Choose a reason for hiding this comment

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

LGTM

@openjdk
Copy link

openjdk bot commented Nov 15, 2022

@jaikiran This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8292317: Missing null check for Iterator.forEachRemaining implementations

Reviewed-by: sundar, smarks

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 103 new commits pushed to the master branch:

  • 52494df: 8290845: Consider an alternative item separator for multi-item option values
  • c56c69e: 8285604: closed sun/java2d/GdiRendering/ClipShapeRendering.java failed with "Incorrect color ffeeeeee instead of ff0000ff in pixel (100, 100)"
  • 6fd1442: 8296743: Tighten Class.getModifiers spec for array classes
  • 3a15e84: 8297258: Typo in java -help referencing -disable-@files instead of --disable-@files
  • 43ce047: 8178698: javax/sound/midi/Sequencer/MetaCallback.java failed with timeout
  • 035eaee: 8296324: JVMTI GetStackTrace truncates vthread stack trace for agents loaded into running VM
  • 59a308b: 8296632: Write a test to verify the content change of TextArea sends TextEvent
  • 11fc65f: 8023562: [TEST_BUG] java/awt/Mouse/EnterExitEvents/DragWindowTest.java failed on ubuntu 13 and mac 10.11
  • 2c692aa: 8297088: Update LCMS to 2.14
  • 9b4e0e8: 8297080: Remove com/sun/jdi/NashornPopFrameTest.java from the problem list
  • ... and 93 more: https://git.openjdk.org/jdk/compare/873eccde01895de06e2216f6838d52d07188addd...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 15, 2022
Assert.assertThrows(NullPointerException.class, () -> it.forEachRemaining(null));
// verify the iterator didn't advance
Assert.assertTrue("Iterator unexpectedly doesn't have any entry", it.hasNext());
}
Copy link
Member

@stuart-marks stuart-marks Nov 17, 2022

Choose a reason for hiding this comment

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

The checks in Collections.java look good.

The tests can be simplified, I think. The reproducers in the original bug report wrap an empty map in either an unmodifiable map or a checked map, so you could just test them with entrySet().iterator().forEachRemaining(null). Those cases do nothing in the current JDK when they indeed should throw NPE. Probably just test for NPE thrown/not-thrown instead of trying to ascertain the position of the iterator.

Copy link
Member Author

@jaikiran jaikiran Nov 17, 2022

Choose a reason for hiding this comment

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

Hello Stuart, I have now updated the PR to simplify the test as suggested. New test methods continue to fail without the source change and pass with this fix.

@jaikiran
Copy link
Member Author

tier1, tier2, tier3 and java_util tests in JCK have passed with this change.

Comment on lines +1717 to 1718
Objects.requireNonNull(action);
i.forEachRemaining(entryConsumer(action));

Choose a reason for hiding this comment

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

As pointed out in the bug report description, it might be better to add the null check to entryConsumer. That would avoid code duplication for the null checks for all the current callers of entryConsumer.

(This comment only applies here; for CheckedEntrySet below this does not work because it does not call entryConsumer.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hello @Marcono1234, I prefer doing it earliest in the top level method (which specifies the contract) instead of doing it in an internal method that this method calls.

Copy link
Member

@stuart-marks stuart-marks left a comment

Choose a reason for hiding this comment

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

Updated tests look good, thanks.

@jaikiran
Copy link
Member Author

Thank you everyone for the reviews and the suggestions.

@jaikiran
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Nov 19, 2022

Going to push as commit 906f1ca.
Since your change was applied there have been 105 commits pushed to the master branch:

  • 0ec575a: 8297289: problem list runtime/vthread/RedefineClass.java and TestObjectAllocationSampleEvent.java
  • dcb8375: 8245246: Deprecate -profile option in javac
  • 52494df: 8290845: Consider an alternative item separator for multi-item option values
  • c56c69e: 8285604: closed sun/java2d/GdiRendering/ClipShapeRendering.java failed with "Incorrect color ffeeeeee instead of ff0000ff in pixel (100, 100)"
  • 6fd1442: 8296743: Tighten Class.getModifiers spec for array classes
  • 3a15e84: 8297258: Typo in java -help referencing -disable-@files instead of --disable-@files
  • 43ce047: 8178698: javax/sound/midi/Sequencer/MetaCallback.java failed with timeout
  • 035eaee: 8296324: JVMTI GetStackTrace truncates vthread stack trace for agents loaded into running VM
  • 59a308b: 8296632: Write a test to verify the content change of TextArea sends TextEvent
  • 11fc65f: 8023562: [TEST_BUG] java/awt/Mouse/EnterExitEvents/DragWindowTest.java failed on ubuntu 13 and mac 10.11
  • ... and 95 more: https://git.openjdk.org/jdk/compare/873eccde01895de06e2216f6838d52d07188addd...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Nov 19, 2022
@openjdk openjdk bot closed this Nov 19, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 19, 2022
@openjdk
Copy link

openjdk bot commented Nov 19, 2022

@jaikiran Pushed as commit 906f1ca.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@jaikiran jaikiran deleted the 8292317 branch November 8, 2024 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants