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

ProviderManager Should Use CollectionUtils#contains #8695

Conversation

yukihane
Copy link
Contributor

Use stream().anyMatch(Objects::isNull) insead of contains(null), which may throw NullPointerException.

Fixes #8689

@pivotal-issuemaster
Copy link

@yukihane Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@yukihane Thank you for signing the Contributor License Agreement!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 16, 2020
Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

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

Thanks for the quick turnaround, @yukihane! I've left some feedback inline.

@@ -145,7 +145,7 @@ private void checkState() {
throw new IllegalArgumentException(
"A parent AuthenticationManager or a list "
+ "of AuthenticationProviders is required");
} else if (providers.contains(null)) {
} else if (providers.stream().anyMatch(Objects::isNull)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

At the framework level, we find that the Stream API is not fast enough. Would you please use a for loop instead?

Copy link
Member

Choose a reason for hiding this comment

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

In particular we avoid the Stream APIs because they create extra objects which leads to excessive Garbage Collection. This can cause the JDK to pause and deteriorate the performance. See gh-7154

I believe we could use Spring's CollectionUtils.contains

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've understood it and amended to use CollectionUtils.contains.

@@ -102,6 +102,26 @@ public void testStartupFailsIfProvidersNotSetAsVarargs() {
new ProviderManager((AuthenticationProvider) null);
}

@Test
public void testUsingNullNotPermittedList() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this test has the right idea. It seems like the list ought to have a null element, though, to fully describe the use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assumed it already existed.
I've created additional test case.

@jzheaux jzheaux self-assigned this Jun 16, 2020
@jzheaux jzheaux added in: core An issue in spring-security-core status: duplicate A duplicate of another issue type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 16, 2020
@jzheaux jzheaux added this to the 5.4.0-M2 milestone Jun 16, 2020
@yukihane yukihane force-pushed the alternative-null-elements-check branch from 3b7fe27 to a8d910e Compare June 16, 2020 20:45
@jzheaux jzheaux changed the title Replace contains(null), may throw NPE ProviderManager Should Use CollectionUtils.contains Jun 16, 2020
@jzheaux jzheaux changed the title ProviderManager Should Use CollectionUtils.contains ProviderManager Should Use CollectionUtils#contains Jun 16, 2020
@jzheaux
Copy link
Contributor

jzheaux commented Jun 16, 2020

Thanks, @yukihane! This is now merged into master via 5302fb7 and c177b39

@jzheaux jzheaux closed this Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core An issue in spring-security-core status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ProviderManager#checkState() throws NullPointerException
5 participants