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

MemoryLayout::offsetHandle can return a negative offset

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P3
    • 24
    • 24
    • core-libs
    • None

    Description

      Consider this code:

      jshell> var layout = MemoryLayout.structLayout(MemoryLayout.paddingLayout(Long.MAX_VALUE - 1), ValueLayout.JAVA_BYTE.withName("x"));
      layout ==> [x9223372036854775806b1(x)]

      jshell> layout.byteOffset(PathElement.groupElement("x"));
      $4 ==> 9223372036854775806

      jshell> MethodHandle offHandle = layout.byteOffsetHandle(PathElement.groupElement("x"));
      offHandle ==> MethodHandle(long)long

      jshell> offHandle.invoke(42);
      $7 ==> -9223372036854775768


      As can be seen, it is possible to make the method handle returned by "offsetHandle" return a negative value, if the field offset is large and a base offset is provided which make the "add" overflow.

      This doesn't happen with other long-based method handles, such as "MemoryLayout::scaleHandle", which correctly throw ArithmeticException in case of overflow.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: