Issue Details (XML | Word | Printable)

Key: CODEC-61
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Igor Slepchin
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons Codec

Base64.encodeBase64() throws NegativeArraySizeException on large files

Created: 03/Feb/08 07:39 AM   Updated: 13/Jul/09 09:44 PM
Return to search
Component/s: None
Affects Version/s: 1.3
Fix Version/s: 1.4

Time Tracking:
Not Specified

Environment:
java -version
java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Server VM (build 1.5.0_03-b07, mixed mode)

uname -a
Linux HP-FC7-IGOR 2.6.23.8-34.fc7 #1 SMP Thu Nov 22 20:39:56 EST 2007 x86_64 x86_64 x86_64 GNU/Linux

Issue Links:
Reference
 

Resolution Date: 08/Mar/08 07:28 AM


 Description  « Hide
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



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Henri Yandell made changes - 04/Feb/08 04:41 PM
Field Original Value New Value
Fix Version/s 1.3 [ 12311737 ]
Affects Version/s 1.3 [ 12311737 ]
Gary Gregory made changes - 04/Feb/08 08:20 PM
Summary Base64.EncodeBase64() thows NegativeArraySizeException on large files Base64.EncodeBase64() throws NegativeArraySizeException on large files
Henri Yandell made changes - 08/Mar/08 07:28 AM
Resolution Fixed [ 1 ]
Status Open [ 1 ] Closed [ 6 ]
Gary Gregory made changes - 13/Jul/09 09:14 PM
Summary Base64.EncodeBase64() throws NegativeArraySizeException on large files Base64.encodeBase64() throws NegativeArraySizeException on large files
Julius Davies made changes - 13/Jul/09 09:44 PM
Link This issue relates to CODEC-69 [ CODEC-69 ]