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

Make equality checks defensive to null reference #19540

Closed
wants to merge 3 commits into from

Conversation

jason-liu475
Copy link

When I used the "inspection code" function of idea, I found that "the equals method of Object is easy to throw a null pointer exception, and a constant or a valued object should be used to call equals".

…, and a constant or a valued object should be used to call equals
@pivotal-issuemaster
Copy link

@tellmenow Please sign the Contributor License Agreement!

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

See the FAQ for frequently asked questions.

@jason-liu475 jason-liu475 changed the base branch from master to 2.2.x January 6, 2020 06:41
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 6, 2020
@pivotal-issuemaster
Copy link

@jason-liu475 Thank you for signing the Contributor License Agreement!

Copy link
Member

@snicoll snicoll 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 PR but we prefer to apply such automated inspection ourselves as usually, a number of false positives surface. This is the case, with a large number of changes not necessary. There are a few worth discussing though. Can you please revert those I've flagged for a start so that we focus on what seems legit?

@@ -82,7 +82,7 @@ private void writeStackTraceElement(PrintWriter writer, StackTraceElement elemen
writer.printf("\tat %s%n", element.toString());
LockInfo lockInfo = info.getLockInfo();
if (firstElement && lockInfo != null) {
if (element.getClassName().equals(Object.class.getName()) && element.getMethodName().equals("wait")) {
if (element.getClassName().equals(Object.class.getName()) && "wait".equals(element.getMethodName())) {
Copy link
Member

Choose a reason for hiding this comment

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

element.getMethodName() does not return null so that change is not necessary.

@@ -74,7 +74,7 @@ default String getPrefix() {
* @return the path as a servlet URL mapping
*/
default String getServletUrlMapping() {
if (getPath().equals("") || getPath().equals("/")) {
if ("".equals(getPath()) || "/".equals(getPath())) {
Copy link
Member

Choose a reason for hiding this comment

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

getPath does not return null.

@@ -74,7 +74,7 @@ default String getUrlMapping() {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (path.equals("/")) {
if ("/".equals(path)) {
Copy link
Member

Choose a reason for hiding this comment

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

There is a usage of path right above so that change isn't necessary.

@@ -265,7 +265,7 @@ public void setLoadOnStartup(int loadOnStartup) {
}

public String getServletMapping() {
if (this.path.equals("") || this.path.equals("/")) {
if ("".equals(this.path) || "/".equals(this.path)) {
Copy link
Member

Choose a reason for hiding this comment

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

this.path is initialized to a value so this is not necessary.

@@ -306,7 +306,7 @@ private void disableGrabResolvers(List<? extends AnnotatedNode> nodes) {
for (AnnotatedNode classNode : nodes) {
List<AnnotationNode> annotations = classNode.getAnnotations();
for (AnnotationNode node : new ArrayList<>(annotations)) {
if (node.getClassNode().getNameWithoutPackage().equals("GrabResolver")) {
if ("GrabResolver".equals(node.getClassNode().getNameWithoutPackage())) {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't return null either.

@@ -210,7 +210,7 @@

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.project.getPackaging().equals("pom")) {
if ("pom".equals(this.project.getPackaging())) {
Copy link
Member

Choose a reason for hiding this comment

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

packaging has a default so there is no need to do that.

@@ -256,7 +256,7 @@ private int hashCode(String protocol, String file) {

@Override
protected boolean sameFile(URL u1, URL u2) {
if (!u1.getProtocol().equals("jar") || !u2.getProtocol().equals("jar")) {
if (!"jar".equals(u1.getProtocol()) || !"jar".equals(u2.getProtocol())) {
Copy link
Member

Choose a reason for hiding this comment

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

protocol is mandatory

@@ -91,7 +91,7 @@ protected RunArguments resolveJvmArguments() {

private boolean isJava13OrLater() {
for (Method method : String.class.getMethods()) {
if (method.getName().equals("stripIndent")) {
if ("stripIndent".equals(method.getName())) {
Copy link
Member

Choose a reason for hiding this comment

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

No need to do this either.

@@ -303,7 +303,7 @@ private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {

private boolean isSet(ConfigurableEnvironment environment, String property) {
String value = environment.getProperty(property);
return (value != null && !value.equals("false"));
return (value != null && !"false".equals(value));
Copy link
Member

Choose a reason for hiding this comment

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

Can be replaced by Boolean.parseBoolean

Copy link
Author

Choose a reason for hiding this comment

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

!"false".equals(value) not equals s.equalsIgnoreCase("true"),eg.If enter an empty string, the former return true but the latter return false.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, you're right there. Maybe not the best idea. I'll make up my mind as part of polishing this.

@@ -82,10 +82,10 @@ public Object getProperty(String name) {
}

private Object getRandomValue(String type) {
if (type.equals("int")) {
if ("int".equals(type)) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think those are necessary either.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jan 6, 2020
@philwebb philwebb added type: bug A general bug and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Jan 9, 2020
@philwebb philwebb added this to the 2.1.x milestone Jan 9, 2020
@philwebb
Copy link
Member

philwebb commented Jan 9, 2020

At least some of these could be considered potential bugs, especially the fact that ...getURI().getScheme() can return null.

@snicoll snicoll changed the title the equals method of Object is easy to throw a null pointer exception, and a constant or a valued object should be used to call equals Make equality checks defensive to null reference Jan 13, 2020
@snicoll snicoll self-assigned this Jan 13, 2020
snicoll pushed a commit that referenced this pull request Jan 13, 2020
@snicoll snicoll modified the milestones: 2.1.x, 2.1.12 Jan 13, 2020
@snicoll snicoll closed this in 91fff91 Jan 13, 2020
@snicoll
Copy link
Member

snicoll commented Jan 13, 2020

@jason-liu475 thank you for making your first contribution to Spring Boot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants