Skip to content

Commit 5cacf21

Browse files
author
Brian Burkhalter
committed
8316156: ByteArrayInputStream.transferTo causes MaxDirectMemorySize overflow
Reviewed-by: alanb
1 parent 3461c7b commit 5cacf21

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/java.base/share/classes/java/io/ByteArrayInputStream.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @since 1.0
4545
*/
4646
public class ByteArrayInputStream extends InputStream {
47+
private static final int MAX_TRANSFER_SIZE = 128*1024;
4748

4849
/**
4950
* An array of bytes that was provided
@@ -205,8 +206,16 @@ public int readNBytes(byte[] b, int off, int len) {
205206
@Override
206207
public synchronized long transferTo(OutputStream out) throws IOException {
207208
int len = count - pos;
208-
out.write(buf, pos, len);
209-
pos = count;
209+
if (len > 0) {
210+
int nwritten = 0;
211+
while (nwritten < len) {
212+
int nbyte = Integer.min(len - nwritten, MAX_TRANSFER_SIZE);
213+
out.write(buf, pos, nbyte);
214+
pos += nbyte;
215+
nwritten += nbyte;
216+
}
217+
assert pos == count;
218+
}
210219
return len;
211220
}
212221

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 8316156
26+
* @summary Ensure ByteArrayInputStream.transferTo does not cause direct memory
27+
* to overflow MaxDirectMemorySize
28+
* @run junit/othervm -XX:MaxDirectMemorySize=5M ChunkedTransferTo
29+
*/
30+
31+
import java.io.ByteArrayInputStream;
32+
import java.io.FileInputStream;
33+
import java.io.IOException;
34+
import java.nio.channels.Channels;
35+
import java.nio.channels.FileChannel;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.util.Arrays;
39+
import java.util.Random;
40+
41+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
42+
import static java.nio.file.StandardOpenOption.*;
43+
44+
import org.junit.jupiter.api.Test;
45+
46+
public class ChunkedTransferTo {
47+
// this value must exceed MaxDirectMemorySize
48+
private static final int SIZE = 10_000_000;
49+
50+
@Test
51+
public void byteArrayInputStream() throws IOException {
52+
byte[] src = new byte[SIZE];
53+
Random rnd = new Random(System.nanoTime());
54+
rnd.nextBytes(src);
55+
try (ByteArrayInputStream bais = new ByteArrayInputStream(src)) {
56+
Path target = Files.createTempFile("SNA", "FU");
57+
FileChannel fc = FileChannel.open(target, CREATE, WRITE);
58+
bais.transferTo(Channels.newOutputStream(fc));
59+
byte[] dst = new byte[SIZE + 1];
60+
try (FileInputStream fis = new FileInputStream(target.toFile())) {
61+
int n = -1;
62+
if ((n = fis.read(dst)) != SIZE)
63+
throw new RuntimeException(n + " != " + SIZE);
64+
}
65+
Files.delete(target);
66+
if (!Arrays.equals(src, 0, SIZE, dst, 0, SIZE))
67+
throw new RuntimeException("Arrays are not equal");
68+
} catch (OutOfMemoryError oome) {
69+
throw new RuntimeException(oome);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)