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

MethodHandles.collectArguments does not throw IAE if pos is outside the arity range

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 17
    • 8, 11, 16
    • core-libs
    • None
    • b07
    • generic
    • generic

      It is noticed that MethodHandles.collectArguments does not follow its documentation:
       
      https://download.java.net/java/early_access/jdk16/docs/api/java.base/java/lang/invoke/MethodHandles.html#collectArguments(java.lang.invoke.MethodHandle,int,java.lang.invoke.MethodHandle)
       
      Specifically, it does not thrown an IllegalArgumentException when "pos is not between 0 and the target's arity".
       
      Here is a micro test (MHCollectArgumentsBug.java), which highlights the bug:

          import java.lang.invoke.MethodHandles;
          import java.lang.invoke.MethodHandle;
          import java.lang.invoke.MethodType;
       
          public class MHCollectArgumentsBug {
             public static void main(String args[]) throws Throwable {
                MethodType II_I_type = MethodType.methodType(int.class, int.class, int.class);
                MethodType S_I_type = MethodType.methodType(int.class, String.class);
       
                MethodHandle target = MethodHandles.publicLookup().findStatic(MHCollectArgumentsBug.class, "subtract", II_I_type);
                MethodHandle filter = MethodHandles.publicLookup().findStatic(MHCollectArgumentsBug.class, "argStringReturnInt1", S_I_type);
       
                System.out.println("Invalid pos (2)");
       
                try {
                   MethodHandles.collectArguments(target, 2, filter);
                } catch (Throwable x) {
                   x.printStackTrace();
                }
             }
       
             public static int subtract(int x, int y) { return x - y; }
             public static int argStringReturnInt1(String s) { return 1; }
          }
       
      Current reference implementation behaviour:
       
      java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
      at java.base/java.lang.invoke.MethodType.parameterType(MethodType.java:775)
      at java.base/java.lang.invoke.MethodHandles.collectArgumentsChecks(MethodHandles.java:5520)
      at java.base/java.lang.invoke.MethodHandles.collectArguments(MethodHandles.java:5498)
      at MHCollectArgumentsBug.main(MHCollectArgumentsBug.java:19)

       
      Expected behaviour:
       
      java.lang.IllegalArgumentException: Filter argument index (pos) is not between 0 and target arity ("2")
          at java.base/java.lang.invoke.MethodHandles.collectArguments(MethodHandles.java)

            mchung Mandy Chung (Inactive)
            rpatil Ramanand Patil (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: