RFR: 6187113: DefaultListSelectionModel.removeIndexInterval(0, Integer.MAX_VALUE) fails
Prasanta Sadhukhan
psadhukhan at openjdk.org
Wed Sep 28 09:27:21 UTC 2022
On Tue, 27 Sep 2022 19:42:01 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:
>> DefaultListSelectionModel.removeIndexInterva accepts `int` value which allows it to take in Integer.MAX_VALUE theoratically but it does calculation with that value which can results in IOOBE.
>> Fix is to make sure the calculation stays within bounds.
>
> 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.
-------------
PR: https://git.openjdk.org/jdk/pull/10409
More information about the client-libs-dev
mailing list