-
Bug
-
Resolution: Unresolved
-
P4
-
5.0
-
None
Consider:
var pq = new PriorityQueue<Number>(new TreeSet<>(Integer::compare))
pq.addAll(Arrays.asList(1.0, 2.0))
==> ClassCastException
The problem is that the copy constructor used above takes a SortedSet<? extends E> which allows the PQ type arg to be a supertype of the SortedSet's type arg. However, the constructor then does
this.comparator = (Comparator<? super E>) c.comparator();
The arg's comparator is Comparator<Integer> but this casts it to Comparator<? super Number> which is clearly unsafe. (Unchecked warnings are suppressed for this entire class.)
A similar problem can occur in the copy constructor that takes a Collection; in that case an instanceof/downcast to SortedSet is performed before the comparator is fetched; the latter is also unsafe.
var pq = new PriorityQueue<Number>(new TreeSet<>(Integer::compare))
pq.addAll(Arrays.asList(1.0, 2.0))
==> ClassCastException
The problem is that the copy constructor used above takes a SortedSet<? extends E> which allows the PQ type arg to be a supertype of the SortedSet's type arg. However, the constructor then does
this.comparator = (Comparator<? super E>) c.comparator();
The arg's comparator is Comparator<Integer> but this casts it to Comparator<? super Number> which is clearly unsafe. (Unchecked warnings are suppressed for this entire class.)
A similar problem can occur in the copy constructor that takes a Collection; in that case an instanceof/downcast to SortedSet is performed before the comparator is fetched; the latter is also unsafe.
- relates to
-
JDK-8181754 Collections.emptyNavigableSet/Map do not specify the comparator
-
- Open
-