8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
authorrraghavan
Thu, 17 Nov 2016 01:17:26 -0800
changeset 42679 25fec8839946
parent 42678 9873beb737e9
child 42680 be69206c1283
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0 Summary: Handled 0 length input case in Java wrapper method Reviewed-by: alanb, ascarpino, kvn, sherman, thartmann
jdk/src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
jdk/src/java.base/share/classes/com/sun/crypto/provider/CounterMode.java
jdk/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java
--- a/jdk/src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java	Wed Nov 09 13:37:19 2016 +0100
+++ b/jdk/src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java	Thu Nov 17 01:17:26 2016 -0800
@@ -142,6 +142,9 @@
      */
     int encrypt(byte[] plain, int plainOffset, int plainLen,
                 byte[] cipher, int cipherOffset) {
+        if (plainLen <= 0) {
+            return plainLen;
+        }
         cryptBlockSizeCheck(plainLen);
         cryptNullAndBoundsCheck(plain, plainOffset, plainLen);
         cryptNullAndBoundsCheck(cipher, cipherOffset, plainLen);
@@ -190,6 +193,9 @@
      */
     int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
                 byte[] plain, int plainOffset) {
+        if (cipherLen <= 0) {
+            return cipherLen;
+        }
         cryptBlockSizeCheck(cipherLen);
         cryptNullAndBoundsCheck(cipher, cipherOffset, cipherLen);
         cryptNullAndBoundsCheck(plain, plainOffset, cipherLen);
@@ -220,10 +226,6 @@
     }
 
     private static void cryptNullAndBoundsCheck(byte[] array, int offset, int len) {
-        if (len <= 0) {
-            return; // not an error because cryptImpl/decryptImpl won't execute if len <= 0
-        }
-
         Objects.requireNonNull(array);
 
         if (offset < 0 || offset >= array.length) {
--- a/jdk/src/java.base/share/classes/com/sun/crypto/provider/CounterMode.java	Wed Nov 09 13:37:19 2016 +0100
+++ b/jdk/src/java.base/share/classes/com/sun/crypto/provider/CounterMode.java	Thu Nov 17 01:17:26 2016 -0800
@@ -172,10 +172,12 @@
      * are encrypted on demand.
      */
     private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
-
-      Objects.checkFromIndexSize(inOff, len, in.length);
-      Objects.checkFromIndexSize(outOff, len, out.length);
-      return implCrypt(in, inOff, len, out, outOff);
+        if (len == 0) {
+            return 0;
+        }
+        Objects.checkFromIndexSize(inOff, len, in.length);
+        Objects.checkFromIndexSize(outOff, len, out.length);
+        return implCrypt(in, inOff, len, out, outOff);
     }
 
     // Implementation of crpyt() method. Possibly replaced with a compiler intrinsic.
--- a/jdk/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java	Wed Nov 09 13:37:19 2016 +0100
+++ b/jdk/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java	Thu Nov 17 01:17:26 2016 -0800
@@ -157,6 +157,9 @@
         // Method possible replaced with a compiler intrinsic.
         private static int encodeISOArray(char[] sa, int sp,
                                           byte[] da, int dp, int len) {
+            if (len <= 0) {
+                return 0;
+            }
             encodeISOArrayCheck(sa, sp, da, dp, len);
             return implEncodeISOArray(sa, sp, da, dp, len);
         }
@@ -177,10 +180,6 @@
 
         private static void encodeISOArrayCheck(char[] sa, int sp,
                                                 byte[] da, int dp, int len) {
-            if (len <= 0) {
-                return;  // not an error because encodeISOArrayImpl won't execute if len <= 0
-            }
-
             Objects.requireNonNull(sa);
             Objects.requireNonNull(da);