Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8288534

Out of bound errors for memory segment access mentions wrong values

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 19
    • 19
    • core-libs
    • None

        Consider this program:

        public class TestOOB {
            public static void main(String[] args) {
                var segment = MemorySegment.allocateNative(10, MemorySession.global());
                segment.getAtIndex(ValueLayout.JAVA_INT, 2);
            }
        }

        There is an out of bound access here, as we are accessing segment at offset 8 with access length 4 (an int) - but the segment is only 10 bytes long.

        The exception coming out of the API is as follows:

        java.lang.IndexOutOfBoundsException: Index 8 out of bounds for length 7

        Note that, while the offset is correct, the access length is misreported. This is due to the fact that the implementation subtracts quantities, to avoid overflow.

        In some cases (access length > segment size) one can even observe negative values in the message.

              mcimadamore Maurizio Cimadamore
              mcimadamore Maurizio Cimadamore
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: