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

SequenceLayout::withElementCount(long elementCount) doesn't throw IllegalArgumentException - if elementCount < 0 for some cases

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 21
    • 21
    • core-libs

    Backports

      Description

        Specification states:

        withElementCountCopy SequenceLayout withElementCount(long elementCount)
        Throws: IllegalArgumentException - if elementCount < 0.

        Found:
        withElementCount doesn't throw IAE if "long" overflow happens.
        The method should be fully documented i.e. AE should be either mentioned or wrapped.

        jshell> SequenceLayout sl = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(4,ValueLayout.JAVA_LONG));
        sl ==> [4:[4:j64]]

        jshell> sl.withElementCount(Long.MIN_VALUE/(7*32));
        | Exception java.lang.ArithmeticException: long overflow
        | at Math.multiplyExact (Math.java:1032)
        | at SequenceLayoutImpl.<init> (SequenceLayoutImpl.java:43)
        | at SequenceLayoutImpl.withElementCount (SequenceLayoutImpl.java:71)
        | at (#31:1)

        also it would be good to wrap ArithmeticException: long overflow in IAE for positive counts:

        jshell> sl.withElementCount(Long.MAX_VALUE/(7*32));
        | Exception java.lang.ArithmeticException: long overflow
        | at Math.multiplyExact (Math.java:1032)
        | at SequenceLayoutImpl.<init> (SequenceLayoutImpl.java:43)
        | at SequenceLayoutImpl.withElementCount (SequenceLayoutImpl.java:71)
        | at (#34:1)

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                  Created:
                  Updated:
                  Resolved: