Skip to content

Commit a39385a

Browse files
committed
IO-356 Fix infinite loop; check that buffer size is large enough to hold any character in the charset
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1470351 13f79535-47bb-0310-9956-ffa450edef68
1 parent 047c234 commit a39385a

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@ public class CharSequenceInputStream extends InputStream {
5555
* @param cs the input character sequence
5656
* @param charset the character set name to use
5757
* @param bufferSize the buffer size to use.
58+
* @throws IllegalArgumentException if the buffer is not large enough to hold a complete character
5859
*/
5960
public CharSequenceInputStream(final CharSequence cs, final Charset charset, final int bufferSize) {
6061
super();
6162
this.encoder = charset.newEncoder()
6263
.onMalformedInput(CodingErrorAction.REPLACE)
6364
.onUnmappableCharacter(CodingErrorAction.REPLACE);
65+
// Ensure that buffer is long enough to hold a complete character
66+
final float maxBytesPerChar = encoder.maxBytesPerChar();
67+
if (bufferSize < maxBytesPerChar) {
68+
throw new IllegalArgumentException("Buffer size " + bufferSize + " is less than maxBytesPerChar " + maxBytesPerChar);
69+
}
6470
this.bbuf = ByteBuffer.allocate(bufferSize);
6571
this.bbuf.flip();
6672
this.cbuf = CharBuffer.wrap(cs);
@@ -73,6 +79,7 @@ public CharSequenceInputStream(final CharSequence cs, final Charset charset, fin
7379
* @param cs the input character sequence
7480
* @param charset the character set name to use
7581
* @param bufferSize the buffer size to use.
82+
* @throws IllegalArgumentException if the buffer is not large enough to hold a complete character
7683
*/
7784
public CharSequenceInputStream(final CharSequence cs, final String charset, final int bufferSize) {
7885
this(cs, Charset.forName(charset), bufferSize);
@@ -84,6 +91,7 @@ public CharSequenceInputStream(final CharSequence cs, final String charset, fina
8491
*
8592
* @param cs the input character sequence
8693
* @param charset the character set name to use
94+
* @throws IllegalArgumentException if the buffer is not large enough to hold a complete character
8795
*/
8896
public CharSequenceInputStream(final CharSequence cs, final Charset charset) {
8997
this(cs, charset, BUFFER_SIZE);
@@ -95,6 +103,7 @@ public CharSequenceInputStream(final CharSequence cs, final Charset charset) {
95103
*
96104
* @param cs the input character sequence
97105
* @param charset the character set name to use
106+
* @throws IllegalArgumentException if the buffer is not large enough to hold a complete character
98107
*/
99108
public CharSequenceInputStream(final CharSequence cs, final String charset) {
100109
this(cs, charset, BUFFER_SIZE);
@@ -112,9 +121,6 @@ private void fillBuffer() throws CharacterCodingException {
112121
if (result.isError()) {
113122
result.throwException();
114123
}
115-
// if (result.isUnderflow()) {
116-
// result.throwException();
117-
// }
118124
this.bbuf.flip();
119125
}
120126

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,25 +197,22 @@ public void testIO_356_B10_D20_S0_UTF8() throws Exception {
197197
testIO_356(10, 20, 0, "UTF-8");
198198
}
199199

200-
private void testIO_356_Loop(final String csName) throws Exception {
201-
for (int bufferSize = 1; bufferSize <= 10; bufferSize++) {
200+
private void testIO_356_Loop(final String csName, final int maxBytesPerChar) throws Exception {
201+
for (int bufferSize = maxBytesPerChar; bufferSize <= 10; bufferSize++) {
202202
for (int dataSize = 1; dataSize <= 20; dataSize++) {
203203
testIO_356(bufferSize, dataSize, 0, csName);
204204
}
205205
}
206206
}
207207

208208
@Test
209-
@Ignore
210-
// Infinite loop
211209
public void testIO_356_Loop_UTF16() throws Exception {
212-
testIO_356_Loop("UTF-16");
210+
testIO_356_Loop("UTF-16", 4);
213211
}
214212

215213
@Test
216-
@Ignore
217214
public void testIO_356_Loop_UTF8() throws Exception {
218-
testIO_356_Loop("UTF-8");
215+
testIO_356_Loop("UTF-8", 4);
219216
}
220217

221218
@Test

0 commit comments

Comments
 (0)