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

Base64.getXDecoder().wrap(...).read() doesn't throw exception for an incorrect number of padding chars in the final unit

XMLWordPrintable

    • b91
    • Verified

      The spec on java.util.Base64.Decoder
      http://download.java.net/jdk8/docs/api/java/util/Base64.Decoder.html
      says:
      "If there is padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IllegalArgumentException is thrown during decoding."

      However this assertion is not satisfied for decoding approach using InputStream:

      The following code sample would not throw IOE as expected for decoding invalid stream:

      ---------------------------------------------------------------------------------------------------------------
      import java.io.*;
      import java.nio.charset.StandardCharsets;
      import java.util.Base64;

      public class WrongNumOfPChars {

          public static void main(String[] args) throws IOException {
              final byte[] bytes = "AA=".getBytes(StandardCharsets.US_ASCII);
              final InputStream stream = Base64.getDecoder().wrap(new ByteArrayInputStream(bytes));
              while (stream.read() != -1);
          }
      }


      the following JCK tests will fail due to this issue:

      api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[wrapDecode_endsWithWrongPadding2]
      api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_Basic[wrapDecode_endsWithWrongPadding2]
      api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_URL[wrapDecode_endsWithWrongPadding2]



            sherman Xueming Shen
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: