changeset 7413:e5ce312a5b10

8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character 8006526: Base64.Decoder.decode(String) spec contains a copy-paste mistake Summary: to ignore single non-base64 char in mime decoding Reviewed-by: alanb
author sherman
date Thu, 31 Jan 2013 13:13:14 -0800
parents a5f38e811ab0
children cff8d7768d72
files src/share/classes/java/util/Base64.java test/java/util/Base64/TestBase64.java
diffstat 2 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Base64.java	Thu Jan 31 12:23:04 2013 -0800
+++ b/src/share/classes/java/util/Base64.java	Thu Jan 31 13:13:14 2013 -0800
@@ -696,7 +696,7 @@
          * using the {@link Base64} encoding scheme.
          *
          * <p> An invocation of this method has exactly the same effect as invoking
-         * {@code return decode(src.getBytes(StandardCharsets.ISO_8859_1))}
+         * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))}
          *
          * @param   src
          *          the string to decode
@@ -1014,9 +1014,12 @@
             int len = sl - sp;
             if (len == 0)
                 return 0;
-            if (len < 2)
+            if (len < 2) {
+                if (isMIME && base64[0] == -1)
+                    return 0;
                 throw new IllegalArgumentException(
                     "Input byte[] should at least have 2 bytes for base64 bytes");
+            }
             if (src[sl - 1] == '=') {
                 paddings++;
                 if (src[sl - 2] == '=')
--- a/test/java/util/Base64/TestBase64.java	Thu Jan 31 12:23:04 2013 -0800
+++ b/test/java/util/Base64/TestBase64.java	Thu Jan 31 13:13:14 2013 -0800
@@ -22,7 +22,7 @@
  */
 
 /**
- * @test 4235519 8004212 8005394
+ * @test 4235519 8004212 8005394 8007298
  * @summary tests java.util.Base64
  */
 
@@ -109,6 +109,9 @@
 
         // test return value from decode(ByteBuffer, ByteBuffer)
         testDecBufRet();
+
+        // test single-non-base64 character for mime decoding
+        testSingleNonBase64MimeDec();
     }
 
     private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
@@ -356,6 +359,19 @@
         } catch (IllegalArgumentException iae) {}
     }
 
+    // single-non-base64-char should be ignored for mime decoding, but
+    // iae for basic decoding
+    private static void testSingleNonBase64MimeDec() throws Throwable {
+        for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) {
+            if (Base64.getMimeDecoder().decode(nonBase64).length != 0) {
+                throw new RuntimeException("non-base64 char is not ignored");
+            }
+            try {
+                Base64.getDecoder().decode(nonBase64);
+                throw new RuntimeException("No IAE for single non-base64 char");
+            } catch (IllegalArgumentException iae) {}
+        }
+    }
 
     private static void testDecBufRet() throws Throwable {
         Random rnd = new java.util.Random();