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 5f22162 [IO-716] ReaderInputStream enter infinite loop for too small buffer sizes. 5f22162 is described below commit 5f22162d13b5d4972181491e6bd943b334e09cf7 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Sep 26 11:27:14 2021 -0400 [IO-716] ReaderInputStream enter infinite loop for too small buffer sizes. --- src/changes/changes.xml | 3 +++ .../org/apache/commons/io/input/ReaderInputStream.java | 17 ++++++++++++----- .../apache/commons/io/input/ReaderInputStreamTest.java | 15 ++++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ff5fda0..da2dee0 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -95,6 +95,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="IO-717" dev="ggregory" type="fix" due-to="Marcono1234, Gary Gregory"> Infinite loop in ReaderInputStream instead of throwing exception for CodingErrorAction.REPORT. </action> + <action issue="IO-716" dev="ggregory" type="fix" due-to="Marcono1234, Gary Gregory"> + ReaderInputStream enter infinite loop for too small buffer sizes. + </action> <!-- ADD --> <action dev="ggregory" type="add" due-to="Gary Gregory"> Add BrokenReader.INSTANCE. diff --git a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java index 860043f..de20bcb 100644 --- a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java @@ -86,7 +86,14 @@ import java.util.Objects; public class ReaderInputStream extends InputStream { private static final int DEFAULT_BUFFER_SIZE = 1024; + private static int checkBufferSize(int bufferSize) { + if (bufferSize < 2) { + throw new IllegalArgumentException("Buffer size < 2"); + } + return bufferSize; + } private final Reader reader; + private final CharsetEncoder encoder; /** @@ -101,8 +108,8 @@ public class ReaderInputStream extends InputStream { * buffer provided by the caller. */ private final ByteBuffer encoderOut; - private CoderResult lastCoderResult; + private boolean endOfInput; /** @@ -131,9 +138,9 @@ public class ReaderInputStream extends InputStream { /** * Constructs a new {@link ReaderInputStream}. * - * @param reader the target {@link Reader} - * @param charset the charset encoding - * @param bufferSize the size of the input buffer in number of characters + * @param reader the target {@link Reader}. + * @param charset the charset encoding. + * @param bufferSize the size of the input buffer in number of characters. */ public ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) { // @formatter:off @@ -141,7 +148,7 @@ public class ReaderInputStream extends InputStream { charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), - bufferSize); + checkBufferSize(bufferSize)); // @formatter:on } diff --git a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java index 0dbe868..5c985da 100644 --- a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java @@ -17,8 +17,8 @@ package org.apache.commons.io.input; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.CharArrayReader; import java.io.IOException; @@ -50,6 +50,19 @@ public class ReaderInputStreamTest { private final Random random = new Random(); + @Test + public void testBufferTooSmall() throws IOException { + assertThrows(IllegalArgumentException.class, () -> new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, 1)); + } + + @Test + @Timeout(value = 500, unit = TimeUnit.MILLISECONDS) + public void testBufferSmallest() throws IOException { + try (InputStream in = new ReaderInputStream(new StringReader("\uD800"), StandardCharsets.UTF_8, 2)) { + in.read(); + } + } + /* * Tests https://issues.apache.org/jira/browse/IO-277 */