From f263c808c0d4eba43a0479c5ba738f18f2e37894 Mon Sep 17 00:00:00 2001 From: Suvrat1629 Date: Tue, 6 Jan 2026 17:53:59 +0530 Subject: [PATCH] fix npe --- checker/tests/nullness/Test6872.java | 18 ++++++++++++++++++ .../framework/util/AtmLubVisitor.java | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 checker/tests/nullness/Test6872.java diff --git a/checker/tests/nullness/Test6872.java b/checker/tests/nullness/Test6872.java new file mode 100644 index 000000000000..79f36222d95a --- /dev/null +++ b/checker/tests/nullness/Test6872.java @@ -0,0 +1,18 @@ +import java.util.*; +import org.checkerframework.checker.nullness.qual.*; + +public class Test6872 { + public static Integer compare(Object x, Object y) { + return 0; + } + + public static void test(Collection> p1, Comparator defaultComparator) + throws Exception { + Collections.min( + p1, + switch (1) { + case 1 -> Test6872::compare; + default -> defaultComparator; + }); + } +} diff --git a/framework/src/main/java/org/checkerframework/framework/util/AtmLubVisitor.java b/framework/src/main/java/org/checkerframework/framework/util/AtmLubVisitor.java index 333325d21e72..274c7f729303 100644 --- a/framework/src/main/java/org/checkerframework/framework/util/AtmLubVisitor.java +++ b/framework/src/main/java/org/checkerframework/framework/util/AtmLubVisitor.java @@ -340,13 +340,28 @@ private void lubPrimaryOnBoundedType( TypeMirror typeMirror2 = type2.getUnderlyingType(); for (AnnotationMirror lower1 : type1LowerBoundAnnos) { + if (lower1 == null) { + // Defensive: AnnotationMirrorSet may (incorrectly) contain null entries. Skip them to + // avoid passing null to QualifierHierarchy/AnnotationUtils which expect non-null + // AnnotationMirrors. + continue; + } AnnotationMirror top = qualHierarchy.getTopAnnotation(lower1); // Can't just call isSubtype because it will return false if bounds have // different annotations on component types AnnotationMirror lower2 = qualHierarchy.findAnnotationInHierarchy(type2LowerBoundAnnos, top); + if (lower2 == null) { + // No matching annotation in the same hierarchy for type2; skip this hierarchy. + continue; + } AnnotationMirror upper1 = type1.getEffectiveAnnotationInHierarchy(lower1); AnnotationMirror upper2 = type2.getEffectiveAnnotationInHierarchy(lower1); + if (upper1 == null || upper2 == null) { + // If effective annotations are missing for either side, skip this hierarchy to avoid + // passing null into QualifierHierarchy methods which assume non-null AnnotationMirrors. + continue; + } if (qualHierarchy.isSubtypeShallow(upper2, typeMirror2, upper1, typeMirror1) && qualHierarchy.isSubtypeShallow(upper1, typeMirror1, upper2, typeMirror2)