From 97bc48d455048cfe873da429ac83aec6c0d347a1 Mon Sep 17 00:00:00 2001 From: Greg Estren Date: Fri, 12 Feb 2021 13:41:10 -0800 Subject: [PATCH] Fix NullPointerException in cquery --output=graph. Fixes https://github.com/bazelbuild/bazel/issues/12915. The repro from the bug crashes because these two deps are compared: `Target: @remote_java_tools_linux//:java_tools/src/tools/singlejar/singlejar_local` `Config: d8d0eb07eb92791668ac973282be1523379b0025b22f0ade56b13d519f2feb2a` `Target: @remote_java_tools_linux//:java_tools/src/tools/singlejar/singlejar_local` `Config: null` This is a `cc_binary`: https://github.com/bazelbuild/bazel/blob/0c1257ed4e1b83f8d0f6c79d641f6bfcf4d1cfc4/src/tools/singlejar/BUILD#L92-L93 I have no idea why the graph visitor tags one of its instances with a null configuration. Nevertheless, this code is still safer. Closes #13002. PiperOrigin-RevId: 357259481 --- .../cquery/GraphOutputFormatterCallback.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java index 25bdf03a986266..f73ef1bfc7f423 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java @@ -50,9 +50,18 @@ Iterable 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 (checksum1 == null) { + return -1; + } else if (checksum2 == null) { + return 1; + } else { + return checksum1.compareTo(checksum2); + } }; @Override