8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow

8218265: java/util/Base64/TestEncodingDecodingLength.java failing

Reviewed-by: rriggs, naoto
This commit is contained in:
Nishit Jain 2019-02-06 13:57:19 +05:30
parent 07664f43d4
commit 13a52f3a17
2 changed files with 30 additions and 35 deletions

View File

@ -251,7 +251,7 @@ public class Base64 {
* @return length of the encoded bytes, or -1 if the length overflows
*
*/
private final int outLength(int srclen, boolean throwOOME) {
private final int encodedOutLength(int srclen, boolean throwOOME) {
int len = 0;
try {
if (doPadding) {
@ -286,7 +286,7 @@ public class Base64 {
* encoded bytes.
*/
public byte[] encode(byte[] src) {
int len = outLength(src.length, true); // dst array size
int len = encodedOutLength(src.length, true); // dst array size
byte[] dst = new byte[len];
int ret = encode0(src, 0, src.length, dst);
if (ret != dst.length)
@ -314,7 +314,7 @@ public class Base64 {
* space for encoding all input bytes.
*/
public int encode(byte[] src, byte[] dst) {
int len = outLength(src.length, false); // dst array size
int len = encodedOutLength(src.length, false); // dst array size
if (dst.length < len || len == -1)
throw new IllegalArgumentException(
"Output byte array is too small for encoding all input bytes");
@ -359,7 +359,7 @@ public class Base64 {
* @return A newly-allocated byte buffer containing the encoded bytes.
*/
public ByteBuffer encode(ByteBuffer buffer) {
int len = outLength(buffer.remaining(), true);
int len = encodedOutLength(buffer.remaining(), true);
byte[] dst = new byte[len];
int ret = 0;
if (buffer.hasArray()) {
@ -560,7 +560,7 @@ public class Base64 {
* if {@code src} is not in valid Base64 scheme
*/
public byte[] decode(byte[] src) {
byte[] dst = new byte[outLength(src, 0, src.length, true)];
byte[] dst = new byte[decodedOutLength(src, 0, src.length)];
int ret = decode0(src, 0, src.length, dst);
if (ret != dst.length) {
dst = Arrays.copyOf(dst, ret);
@ -613,7 +613,7 @@ public class Base64 {
* does not have enough space for decoding all input bytes.
*/
public int decode(byte[] src, byte[] dst) {
int len = outLength(src, 0, src.length, false);
int len = decodedOutLength(src, 0, src.length);
if (dst.length < len || len == -1)
throw new IllegalArgumentException(
"Output byte array is too small for decoding all input bytes");
@ -657,7 +657,7 @@ public class Base64 {
sp = 0;
sl = src.length;
}
byte[] dst = new byte[outLength(src, sp, sl, true)];
byte[] dst = new byte[decodedOutLength(src, sp, sl)];
return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
} catch (IllegalArgumentException iae) {
buffer.position(pos0);
@ -691,13 +691,11 @@ public class Base64 {
* @param src the byte array to decode
* @param sp the source position
* @param sl the source limit
* @param throwOOME if true, throws OutOfMemoryError if the length of
* the decoded bytes overflows; else returns the
* length
* @return length of the decoded bytes, or -1 if the length overflows
*
* @return length of the decoded bytes
*
*/
private int outLength(byte[] src, int sp, int sl, boolean throwOOME) {
private int decodedOutLength(byte[] src, int sp, int sl) {
int[] base64 = isURL ? fromBase64URL : fromBase64;
int paddings = 0;
int len = sl - sp;
@ -733,18 +731,12 @@ public class Base64 {
if (paddings == 0 && (len & 0x3) != 0)
paddings = 4 - (len & 0x3);
try {
len = Math.multiplyExact(3, (Math.addExact(len, 3) / 4)) - paddings;
} catch (ArithmeticException ex) {
if (throwOOME) {
throw new OutOfMemoryError("Decoded size is too large");
} else {
// let the caller know that the decoded bytes length
// is too large
len = -1;
}
}
return len;
// If len is near to Integer.MAX_VALUE, (len + 3)
// can possibly overflow, perform this operation as
// long and cast it back to integer when the value comes under
// integer limit. The final value will always be in integer
// limits
return 3 * (int) ((len + 3L) / 4) - paddings;
}
private int decode0(byte[] src, int sp, int sl, byte[] dst) {

View File

@ -22,22 +22,23 @@
*/
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Base64;
/**
* @test
* @bug 8210583
* @summary Tests Base64.Encoder.encode/Decoder.decode for the large size
* of resulting bytes which can not be allocated
* @requires os.maxMemory >= 6g
* @run main/othervm -Xms4g -Xmx6g TestEncodingDecodingLength
* @bug 8210583 8217969 8218265
* @summary Tests Base64.Encoder.encode and Base64.Decoder.decode
* with the large size of input array/buffer
* @requires os.maxMemory >= 8g
* @run main/othervm -Xms6g -Xmx8g TestEncodingDecodingLength
*
*/
public class TestEncodingDecodingLength {
public static void main(String[] args) {
int size = Integer.MAX_VALUE - 2;
int size = Integer.MAX_VALUE - 8;
byte[] inputBytes = new byte[size];
byte[] outputBytes = new byte[size];
@ -46,13 +47,15 @@ public class TestEncodingDecodingLength {
checkOOM("encode(byte[])", () -> encoder.encode(inputBytes));
checkIAE("encode(byte[] byte[])", () -> encoder.encode(inputBytes, outputBytes));
checkOOM("encodeToString(byte[])", () -> encoder.encodeToString(inputBytes));
checkOOM("encode(ByteBuffer)", () -> encoder.encode(ByteBuffer.allocate(size)));
checkOOM("encode(ByteBuffer)", () -> encoder.encode(ByteBuffer.wrap(inputBytes)));
// Check decoder with large array length
// Check decoder with large array length,
// should not throw any exception
Arrays.fill(inputBytes, (byte) 86);
Base64.Decoder decoder = Base64.getDecoder();
checkOOM("decode(byte[])", () -> decoder.decode(inputBytes));
checkIAE("decode(byte[], byte[])", () -> decoder.decode(inputBytes, outputBytes));
checkOOM("decode(ByteBuffer)", () -> decoder.decode(ByteBuffer.allocate(size)));
decoder.decode(inputBytes);
decoder.decode(inputBytes, outputBytes);
decoder.decode(ByteBuffer.wrap(inputBytes));
}
private static final void checkOOM(String methodName, Runnable r) {