8058291: Missing some checks during parameter validation
authorvlivanov
Tue, 16 Sep 2014 18:05:01 +0400
changeset 26625 cbeb3711fa94
parent 26624 e6383382c939
child 26626 14bb608ce64e
8058291: Missing some checks during parameter validation Reviewed-by: jrose
jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java
jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
jdk/src/java.base/share/classes/java/lang/invoke/MethodType.java
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java	Tue Sep 16 12:06:53 2014 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java	Tue Sep 16 18:05:01 2014 +0400
@@ -188,7 +188,6 @@
     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
                                             boolean strict, boolean monobox) {
         MethodType dstType = target.type();
-        assert(dstType.parameterCount() == target.type().parameterCount());
         if (srcType == dstType)
             return target;
         if (USE_LAMBDA_FORM_EDITOR) {
@@ -265,6 +264,7 @@
 
     static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType,
                                                     boolean strict, boolean monobox) {
+        assert(target.type().parameterCount() == srcType.parameterCount());
         // Calculate extra arguments (temporaries) required in the names array.
         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
         final int INARG_COUNT = srcType.parameterCount();
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Sep 16 12:06:53 2014 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Sep 16 18:05:01 2014 +0400
@@ -2023,8 +2023,9 @@
      */
     public static
     MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
+        explicitCastArgumentsChecks(target, newType);
+        // use the asTypeCache when possible:
         MethodType oldType = target.type();
-        // use the asTypeCache when possible:
         if (oldType == newType)  return target;
         if (oldType.explicitCastEquivalentToAsType(newType)) {
             return target.asType(newType);
@@ -2032,6 +2033,12 @@
         return MethodHandleImpl.makePairwiseConvert(target, newType, false);
     }
 
+    private static void explicitCastArgumentsChecks(MethodHandle target, MethodType newType) {
+        if (target.type().parameterCount() != newType.parameterCount()) {
+            throw new WrongMethodTypeException("cannot explicitly cast " + target + " to " + newType);
+        }
+    }
+
     /**
      * Produces a method handle which adapts the calling sequence of the
      * given method handle to a new type, by reordering the arguments.
@@ -2479,6 +2486,7 @@
     MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
         MethodType oldType = target.type();  // get NPE
         int dropped = dropArgumentChecks(oldType, pos, valueTypes);
+        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
         if (dropped == 0)  return target;
         BoundMethodHandle result = target.rebind();
         LambdaForm lform = result.form;
@@ -2490,7 +2498,6 @@
         } else {
             lform = lform.addArguments(pos, valueTypes);
         }
-        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
         result = result.copyWith(newType, lform);
         return result;
     }
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodType.java	Tue Sep 16 12:06:53 2014 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodType.java	Tue Sep 16 18:05:01 2014 +0400
@@ -869,9 +869,7 @@
         if (dstTypes == srcTypes) {
             return true;
         }
-        if (dstTypes.length != srcTypes.length) {
-            return false;
-        }
+        assert(dstTypes.length == srcTypes.length);
         for (int i = 0; i < dstTypes.length; i++) {
             if (!explicitCastEquivalentToAsType(srcTypes[i], dstTypes[i])) {
                 return false;