Skip to content

Commit 153f3d1

Browse files
committed
Prevent infinite loop with AbstractCharacterFilterReader if EOF is filtered out
1 parent 8365dcf commit 153f3d1

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public int read() throws IOException {
4242
int ch;
4343
do {
4444
ch = in.read();
45-
} while (filter(ch));
45+
} while (ch != EOF && filter(ch));
4646
return ch;
4747
}
4848

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.apache.commons.io.input;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
2021

2122
import java.io.IOException;
2223
import java.io.StringReader;
24+
import java.time.Duration;
2325
import java.util.HashSet;
2426

2527
import org.apache.commons.io.IOUtils;
@@ -92,4 +94,17 @@ public void testReadUsingReader() throws IOException {
9294
}
9395
}
9496

97+
@Test
98+
public void testReadFilteringEOF() throws IOException {
99+
final StringReader input = new StringReader("ababcabcd");
100+
assertTimeoutPreemptively(Duration.ofMillis(500), () -> {
101+
try (StringBuilderWriter output = new StringBuilderWriter(); CharacterFilterReader reader = new CharacterFilterReader(input, -1)) {
102+
int c;
103+
while ((c = reader.read()) != -1) {
104+
output.write(c);
105+
}
106+
assertEquals("ababcabcd", output.toString());
107+
}
108+
});
109+
}
95110
}

0 commit comments

Comments
 (0)