Commons Codec
  1. Commons Codec
  2. CODEC-61

Base64.encodeBase64() throws NegativeArraySizeException on large files

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.3
    • Fix Version/s: 1.4
    • Labels:
      None
    • Environment:

      Description

      The NegativeArraySizeException exception is thrown by Base64.EncodeBase64() for arrays larger than 268435455 bytes (2^31/8-1).

      public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) starts with the following three lines:

      int lengthDataBits = binaryData.length * EIGHTBIT;
      int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
      int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;

      The first of the lines will cause an integer overflow in lengthDataBits for lengths larger than 2^31/8-1, making it a negative number. The fix is trivial (but not tested on the running code, I just ran through a few numbers to validate that it computes the same results as the original code):

      int lengthData = binaryData.length;
      int fewerThan24bits = lengthData % (TWENTYFOURBITGROUP / EIGHTBIT) * EIGHTBIT;
      int numberTriplets = lengthData / (TWENTYFOURBITGROUP / EIGHTBIT);

      This way the encoder will be able to process files of up to 2^31-1 bytes in length, which is much better than ~250MB.

      The issue was found in commons 1.3; the source code above was taken from SVN trunk so I assume it's still present in 1.4: http://svn.apache.org/repos/asf/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java

        Issue Links

          Activity

          No work has yet been logged on this issue.

            People

            • Assignee:
              Unassigned
              Reporter:
              Igor Slepchin
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development