RFR: 6187113: DefaultListSelectionModel.removeIndexInterval(0, Integer.MAX_VALUE) fails
Alexey Ivanov
aivanov at openjdk.org
Wed Sep 28 09:40:21 UTC 2022
On Wed, 28 Sep 2022 09:23:42 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:
>> src/java.desktop/share/classes/javax/swing/DefaultListSelectionModel.java line 695:
>>
>>> 693: int rmMaxIndex = Math.max(index0, index1);
>>> 694: int gapLength = ((rmMaxIndex - rmMinIndex) + 1) > (rmMaxIndex - rmMinIndex)
>>> 695: ? ((rmMaxIndex - rmMinIndex) + 1) : (rmMaxIndex - rmMinIndex);
>>
>> I wonder if the model supports selections of 0 .. Integer.MAX_VALUE, it should … but it doesn't. If I call
>>
>> selectionModel.setSelectionInterval(Integer.MAX_VALUE - 1, Integer.MAX_VALUE);
>>
>> it never returns, it goes into an infinite loop in `changeSelection` because the condition `i <= Math.max(setMax, clearMax)` is always `true` if either `setMax` or `clearMax` is `Integer.MAX_VALUE`. Therefore, we should prevent that from happening. With that in mind, for `removeIndexInterval`, the value of `Integer.MAX_VALUE` becomes invalid.
>>
>> What will happen if negative values are passed, in particular `Integer.MIN_VALUE`?
>
> For negative values, the spec says "@throws IndexOutOfBoundsException if either index is less than {@code -1}" which it does.
Well, the spec says nothing about it for this method: https://github.com/openjdk/jdk/blob/096bca4a9c5e8ac2668dd965df92153ea1d80add/src/java.desktop/share/classes/javax/swing/DefaultListSelectionModel.java#L684-L690
I guess it should be update to state it explicitly.
-------------
PR: https://git.openjdk.org/jdk/pull/10409
More information about the client-libs-dev
mailing list