JDK-8271865 : SortedList::getViewIndex behaves not correctly for some index values
  • Type: Bug
  • Component: javafx
  • Sub-Component: base
  • Affected Version: jfx11,jfx16,jfx17
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2021-07-25
  • Updated: 2024-03-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Description
A DESCRIPTION OF THE PROBLEM :
Method
public int getViewIndex(int index)
of class javafx.collections.transformation.SortedList<E>
behaves erroneously under certain conditions

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See provided JUnit test method

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
SortedList.getViewIndex(int index) returns a negative value if index is outside of the list
ACTUAL -
SortedList.getViewIndex(int index) returns zero or throws AIOOBE for some index values outside of the list

--- Output of Test Case ---
Source list : [4, 1, 3, 2]
Sorted list : [1, 2, 3, 4]
sortedList.getViewIndex(0) = 3
sortedList.getViewIndex(1) = 0
sortedList.getViewIndex(2) = 2
sortedList.getViewIndex(3) = 1
sortedList.getViewIndex(4) = 0
sortedList.getViewIndex(5) = 0
sortedList.getViewIndex(6) = 0
Index 7 out of bounds for length 7
Index 8 out of bounds for length 7
Index 9 out of bounds for length 7


---------- BEGIN SOURCE ----------
	@Test
	public void test_SortedList_getViewIndex()
	{
		final var sourceList = FXCollections.observableArrayList(4, 1, 3, 2);
		final var sortedList = new SortedList<>(sourceList, Integer::compare);
		System.out.println("Source list : " + sourceList);
		System.out.println("Sorted list : " + sortedList);
		assertEquals(3, sortedList.getViewIndex(0));
		assertEquals(0, sortedList.getViewIndex(1));
		assertEquals(2, sortedList.getViewIndex(2));
		assertEquals(1, sortedList.getViewIndex(3));
		for (int i = 0; i < 10; i++)
		{
			try
			{
				System.out.println("sortedList.getViewIndex(" + i + ") = " + sortedList.getViewIndex(i));
			}
			catch (IndexOutOfBoundsException ex)
			{
				System.out.println(ex.getMessage());
			}
		}
		// the following tests fail:
		assertTrue(sortedList.getViewIndex(4) < 0);
		assertTrue(sortedList.getViewIndex(5) < 0);
		assertTrue(sortedList.getViewIndex(6) < 0);
		assertTrue(sortedList.getViewIndex(7) < 0);
		assertTrue(sortedList.getViewIndex(8) < 0);
		assertTrue(sortedList.getViewIndex(9) < 0);
	}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Check index before calling the getViewIndex(int index) method

FREQUENCY : always



Comments
A pull request was submitted for review. URL: https://git.openjdk.org/jfx/pull/1432 Date: 2024-03-24 15:11:29 +0000
24-03-2024

I can reproduce this. It looks like the getViewIndex method isn't checking the size of the list, but just using whatever happens to be allocated, even if it's past the end of the in-use elements. There is a somewhat related bug in getSourceIndex. That method should throw an IOOBE (although it isn't documented to do so) for any index out of range in the sorted list. Instead, we sometimes get an NPE rather than an IOOBE if the index is >= size but < capacity.
04-12-2021

Checked with attached JUnit test case, Issue is reproducible < attached screenshot for reference> Test Result: ========= openjfx11 Fail openjfx16 Fail openjfx17ea15 Fail
04-08-2021