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

Fix NullPointerException in cquery --output=graph. #13002

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.Objects;

/** cquery output formatter that prints the result as factored graph in AT&T GraphViz format. */
class GraphOutputFormatterCallback extends CqueryThreadsafeCallback {
Expand All @@ -50,9 +51,20 @@ Iterable<KeyedConfiguredTarget> getDirectDeps(KeyedConfiguredTarget target)
// Order graph output first by target label, then by configuration hash.
Label label1 = ct1.getLabel();
Label label2 = ct2.getLabel();
return label1.equals(label2)
? ct1.getConfigurationChecksum().compareTo(ct2.getConfigurationChecksum())
: label1.compareTo(label2);
if (!label1.equals(label2)) {
return label1.compareTo(label2);
}
String checksum1 = ct1.getConfigurationChecksum();
String checksum2 = ct2.getConfigurationChecksum();
if (Objects.equals(checksum1, checksum2)) {
Copy link
Member

Choose a reason for hiding this comment

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

This is the same as the compareTo below, and the checksum2 null check isn't needed, either. I'd simplify this as:

if (checksum1 == null) {
return -1;
} else {
return checksum1.compareTo(checksum2);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How's this? I left the double null check because of this comment from https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html:

Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or am I missing something about the null check?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I hadn't realized that. Okay, the second null check is necessary, then.

return 0;
} else if (checksum1 == null) {
return -1;
} else if (checksum2 == null) {
return 1;
} else {
return checksum1.compareTo(checksum2);
}
};

@Override
Expand Down