You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/07/04 23:02:24 UTC
(commons-io) branch master updated: Fix infinite loop in AbstractByteArrayOutputStream. (#758)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new 0accfaa6b Fix infinite loop in AbstractByteArrayOutputStream. (#758)
0accfaa6b is described below
commit 0accfaa6bfc5be4a0aada228aa5845ae1b74d3f9
Author: Alex Benusovich <qw...@gmail.com>
AuthorDate: Fri Jul 4 16:02:20 2025 -0700
Fix infinite loop in AbstractByteArrayOutputStream. (#758)
When an AbstractByteArrayOutputStream is initialized with a 0 size buffer and writeImpl(InputStream) is called an infinite loop may occur and eventually cause an OOM exception.
This is due to passing 0 as the maximum bytes to read in InputStream#read which returns 0, then a new 0 size buffer is created, and then InputStream#read is called again therefore repeating the loop forever.
Fix this infinite loop by updating needNewBuffer to use DEFAULT_SIZE as the initial buffer size instead of 0.
This will allow InputStream#read to properly populate the buffer as expected and terminate the loop when EOF is reached.
Set initial value of currentBufferIndex to -1 so it is properly initialized to 0 when needNewBuffer is called; unrelated to the infinite loop bug.
Co-authored-by: Alex Benusovich <ab...@revolutionehr.com>
---
.../io/output/AbstractByteArrayOutputStream.java | 5 ++--
.../io/output/ByteArrayOutputStreamTest.java | 31 +++++++++++++++++++---
2 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java b/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java
index 8f959751d..b8b90b651 100644
--- a/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java
@@ -92,7 +92,7 @@ protected interface InputStreamConstructor<T extends InputStream> {
private byte[] currentBuffer;
/** The index of the current buffer. */
- private int currentBufferIndex;
+ private int currentBufferIndex = -1;
/** The total count of bytes in all the filled buffers. */
private int filledBufferSum;
@@ -147,7 +147,8 @@ protected void needNewBuffer(final int newCount) {
// Creating new buffer
final int newBufferSize;
if (currentBuffer == null) {
- newBufferSize = newCount;
+ // prevents 0 size buffers
+ newBufferSize = newCount > 0 ? newCount : DEFAULT_SIZE;
filledBufferSum = 0;
} else {
newBufferSize = Math.max(currentBuffer.length << 1, newCount - filledBufferSum);
diff --git a/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTest.java
index 791d74e85..0274d8bca 100644
--- a/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTest.java
@@ -28,12 +28,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.function.IOFunction;
import org.apache.commons.io.input.ClosedInputStream;
+import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@@ -100,14 +103,19 @@ private static boolean byteCmp(final byte[] src, final byte[] cmp) {
private static Stream<Arguments> toBufferedInputStreamFunctionFactories() {
final IOFunction<InputStream, InputStream> syncBaosToBufferedInputStream = ByteArrayOutputStream::toBufferedInputStream;
final IOFunction<InputStream, InputStream> syncBaosToBufferedInputStreamWithSize = is -> ByteArrayOutputStream.toBufferedInputStream(is, 1024);
+ final IOFunction<InputStream, InputStream> syncBaosToBufferedInputStreamWith0Size = is -> ByteArrayOutputStream.toBufferedInputStream(is, 0);
final IOFunction<InputStream, InputStream> unSyncBaosToBufferedInputStream = UnsynchronizedByteArrayOutputStream::toBufferedInputStream;
- final IOFunction<InputStream, InputStream> unSyncBaosToBufferedInputStreamWithSize = is -> UnsynchronizedByteArrayOutputStream.toBufferedInputStream(is,
- 1024);
+ final IOFunction<InputStream, InputStream> unSyncBaosToBufferedInputStreamWithSize = is -> UnsynchronizedByteArrayOutputStream.toBufferedInputStream(
+ is, 1024);
+ final IOFunction<InputStream, InputStream> unSyncBaosToBufferedInputStreamWith0Size = is -> UnsynchronizedByteArrayOutputStream.toBufferedInputStream(
+ is, 0);
return Stream.of(Arguments.of("ByteArrayOutputStream.toBufferedInputStream(InputStream)", syncBaosToBufferedInputStream),
Arguments.of("ByteArrayOutputStream.toBufferedInputStream(InputStream, int)", syncBaosToBufferedInputStreamWithSize),
+ Arguments.of("ByteArrayOutputStream.toBufferedInputStream(InputStream, 0)", syncBaosToBufferedInputStreamWith0Size),
Arguments.of("UnsynchronizedByteArrayOutputStream.toBufferedInputStream(InputStream)", unSyncBaosToBufferedInputStream),
- Arguments.of("UnsynchronizedByteArrayOutputStream.toBufferedInputStream(InputStream, int)", unSyncBaosToBufferedInputStreamWithSize));
+ Arguments.of("UnsynchronizedByteArrayOutputStream.toBufferedInputStream(InputStream, int)", unSyncBaosToBufferedInputStreamWithSize),
+ Arguments.of("UnsynchronizedByteArrayOutputStream.toBufferedInputStream(InputStream, 0)", unSyncBaosToBufferedInputStreamWith0Size));
}
private void checkByteArrays(final byte[] expected, final byte[] actual) {
@@ -184,7 +192,6 @@ void testToBufferedInputStream(final String baosName, final IOFunction<InputStre
assertEquals(data.length, buffered.available());
assertArrayEquals(data, IOUtils.toByteArray(buffered));
-
}
}
}
@@ -198,7 +205,23 @@ void testToBufferedInputStreamEmpty(final String baosName, final IOFunction<Inpu
try (InputStream buffered = toBufferedInputStreamFunction.apply(bain)) {
assertEquals(0, buffered.available());
+ }
+ }
+ }
+
+ @ParameterizedTest(name = "[{index}] {0}")
+ @MethodSource("toBufferedInputStreamFunctionFactories")
+ void testToBufferedInputStreamEmptyFile(final String baosName, final IOFunction<InputStream, InputStream> toBufferedInputStreamFunction,
+ final @TempDir Path temporaryFolder) throws IOException {
+ final Path emptyFile = Files.createTempFile(temporaryFolder, getClass().getSimpleName(), "-empty.txt");
+
+ try (InputStream is = Files.newInputStream(emptyFile)) {
+ assertEquals(0, is.available());
+
+ try (InputStream buffered = toBufferedInputStreamFunction.apply(is)) {
+ assertEquals(0, buffered.available());
+ assertArrayEquals(IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(buffered));
}
}
}