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

CharsetDecoder.decode(ByteBuffer) throws IllegalArgumentException

XMLWordPrintable

    • b16
    • generic
    • generic
    • Verified

      A DESCRIPTION OF THE PROBLEM :
      If CharsetDecoder.decode(ByteBuffer) is called on a ByteBuffer with size close to Integer.MAX_VALUE, IllegalArgumentException is thrown due to integer overflow in output buffer size allocation.

      In most cases (characters per byte 1.0 or less), decode() should handle up to Integer.MAX_VALUE sized ByteBuffers. In cases where characters per byte can be greater than one and output size would exceed Integer.MAX_VALUE, then perhaps some new subclass of CharacterCodingException, such as "OutputSizeException" could be thrown.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run as:
         java -Xms8G -Xmx8G CharsetDecoderBug


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No output.
      ACTUAL -
      Exception in thread "main" java.lang.IllegalArgumentException: capacity < 0: (-2000127 < 0)
      at java.base/java.nio.Buffer.createCapacityException(Buffer.java:278)
      at java.base/java.nio.CharBuffer.allocate(CharBuffer.java:360)
      at java.base/java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:814)
      at CharsetDecoderBug.main(CharsetDecoderBug.java:14)


      ---------- BEGIN SOURCE ----------
      import java.nio.charset.*;
      import java.nio.*;

      public class CharsetDecoderBug {
         public static void main(String[] args) {
            Charset charset = Charset.forName("utf8");
            CharsetDecoder decoder = charset.newDecoder();
            // This works. // int size = Integer.MAX_VALUE - 10000000;
            int size = Integer.MAX_VALUE - 1000000;
            byte[] bytes = new byte[size];
            ByteBuffer bb = ByteBuffer.wrap(bytes, 0, size);
            try {
               decoder.decode(bb);
            }
            catch (CharacterCodingException ignored) {
            }
         }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Enforce a lower limit on the size of the ByteBuffer before calling decode().

      FREQUENCY : always


            naoto Naoto Sato
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: