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

java.lang.classfile.Signature.ArrayTypeSig.of IAE not thrown for dims > 255

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 25
    • core-libs
    • None
    • behavioral
    • minimal
    • Minor javadoc wording fix represents no compatibility risk.
    • Java API
    • SE

      Summary

      Allign java.lang.classfile.Signature.ArrayTypeSig::of constraints check with the javadoc.

      Problem

      • Javadoc of java.lang.classfile.Signature.ArrayTypeSig::of says: "Throws: IllegalArgumentException - if the resulting array type exceeds 255 dimensions". However the constraint check allows to exceed that limit when the componentSignature argument is already an array.
      • The javadoc does not mention the IllegalArgumentException is thrown when dims < 1.

      Solution

      Fix the constraints check and the javadoc.

      Specification

      --- a/src/java.base/share/classes/java/lang/classfile/Signature.java
      +++ b/src/java.base/share/classes/java/lang/classfile/Signature.java
      @@ -417,15 +417,18 @@ public static ArrayTypeSig of(Signature componentSignature) {
                * {@return a signature for an array type}
                * @param dims the dimension of the array
                * @param componentSignature the component type
      -         * @throws IllegalArgumentException if the resulting array type exceeds
      -         *         255 dimensions
      +         * @throws IllegalArgumentException if {@code dims < 1} or the
      +         *         resulting array type exceeds 255 dimensions
                */
               public static ArrayTypeSig of(int dims, Signature componentSignature) {
                   requireNonNull(componentSignature);
      +            if (componentSignature instanceof SignaturesImpl.ArrayTypeSigImpl arr) {
      +                if (dims < 1 || dims > 255 - arr.arrayDepth())
      +                    throw new IllegalArgumentException("illegal array depth value");
      +                return new SignaturesImpl.ArrayTypeSigImpl(dims + arr.arrayDepth(), arr.elemType());
      +            }
                   if (dims < 1 || dims > 255)
                       throw new IllegalArgumentException("illegal array depth value");
      -            if (componentSignature instanceof SignaturesImpl.ArrayTypeSigImpl arr)
      -                return new SignaturesImpl.ArrayTypeSigImpl(dims + arr.arrayDepth(), arr.elemType());
                   return new SignaturesImpl.ArrayTypeSigImpl(dims, componentSignature);
               }
           }

            asotona Adam Sotona
            pgundarlahal Prashanthram Gundarlahally
            Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: