Skip to content

Commit d326857

Browse files
author
Gary Gregory
committed
[IO-717] Infinite loop in ReaderInputStream instead of throwing
exception for CodingErrorAction.REPORT.
1 parent 73c3f98 commit d326857

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ The <action> type attribute can be add,update,fix,remove.
9292
<action issue="IO-721" dev="ggregory" type="fix" due-to="Dirk Heinrichs, Gary Gregory">
9393
Wrong exception message in FileUtils.setLastModified(File, File).
9494
</action>
95+
<action issue="IO-717" dev="ggregory" type="fix" due-to="Marcono1234, Gary Gregory">
96+
Infinite loop in ReaderInputStream instead of throwing exception for CodingErrorAction.REPORT.
97+
</action>
9598
<!-- ADD -->
9699
<action dev="ggregory" type="add" due-to="Gary Gregory">
97100
Add BrokenReader.INSTANCE.

src/main/java/org/apache/commons/io/input/ReaderInputStream.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ private void fillBuffer() throws IOException {
228228
}
229229
encoderOut.compact();
230230
lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput);
231+
if (lastCoderResult.isError()) {
232+
lastCoderResult.throwException();
233+
}
231234
encoderOut.flip();
232235
}
233236

src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,30 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

2223
import java.io.CharArrayReader;
2324
import java.io.IOException;
25+
import java.io.InputStream;
2426
import java.io.StringReader;
27+
import java.nio.charset.CharacterCodingException;
2528
import java.nio.charset.Charset;
29+
import java.nio.charset.CharsetEncoder;
30+
import java.nio.charset.CodingErrorAction;
2631
import java.nio.charset.StandardCharsets;
2732
import java.util.Random;
33+
import java.util.concurrent.TimeUnit;
2834

2935
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.api.Timeout;
3037

3138
public class ReaderInputStreamTest {
3239
private static final String TEST_STRING = "\u00e0 peine arriv\u00e9s nous entr\u00e2mes dans sa chambre";
3340
private static final String LARGE_TEST_STRING;
3441

3542
static {
3643
final StringBuilder buffer = new StringBuilder();
37-
for (int i=0; i<100; i++) {
44+
for (int i = 0; i < 100; i++) {
3845
buffer.append(TEST_STRING);
3946
}
4047
LARGE_TEST_STRING = buffer.toString();
@@ -48,7 +55,7 @@ public class ReaderInputStreamTest {
4855
@Test
4956
public void testCharsetMismatchInfiniteLoop() throws IOException {
5057
// Input is UTF-8 bytes: 0xE0 0xB2 0xA0
51-
final char[] inputChars = { (char) 0xE0, (char) 0xB2, (char) 0xA0 };
58+
final char[] inputChars = {(char) 0xE0, (char) 0xB2, (char) 0xA0};
5259
// Charset charset = Charset.forName("UTF-8"); // works
5360
final Charset charset = StandardCharsets.US_ASCII; // infinite loop
5461
try (ReaderInputStream stream = new ReaderInputStream(new CharArrayReader(inputChars), charset)) {
@@ -57,6 +64,20 @@ public void testCharsetMismatchInfiniteLoop() throws IOException {
5764
}
5865
}
5966

67+
/**
68+
* Tests IO-717 to avoid infinite loops.
69+
*
70+
* ReaderInputStream does not throw exception with {@link CodingErrorAction#REPORT}.
71+
*/
72+
@Test
73+
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
74+
public void testCodingErrorAction() throws IOException {
75+
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder().onMalformedInput(CodingErrorAction.REPORT);
76+
try (InputStream in = new ReaderInputStream(new StringReader("\uD800aa"), encoder, 2)) {
77+
assertThrows(CharacterCodingException.class, in::read);
78+
}
79+
}
80+
6081
@Test
6182
public void testLargeUTF8WithBufferedRead() throws IOException {
6283
testWithBufferedRead(LARGE_TEST_STRING, "UTF-8");

0 commit comments

Comments
 (0)