Skip to content

Commit aa9b8f0

Browse files
committedAug 22, 2022
8292043: Incorrect decoding near EOF for stateful decoders like UTF-16
Reviewed-by: joehw, alanb, lancea
1 parent f95ee79 commit aa9b8f0

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed
 

‎src/java.base/share/classes/sun/nio/cs/StreamDecoder.java

-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ int implRead(char[] cbuf, int off, int end) throws IOException {
378378
eof = true;
379379
if ((cb.position() == 0) && (!bb.hasRemaining()))
380380
break;
381-
decoder.reset();
382381
}
383382
continue;
384383
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 8292043
26+
* @run testng StatefulDecoderNearEOF
27+
* @summary Check MalformedInputException is thrown with stateful decoders
28+
* with malformed input before EOF
29+
*/
30+
31+
import java.io.ByteArrayInputStream;
32+
import java.io.IOException;
33+
import java.io.InputStreamReader;
34+
import java.io.UncheckedIOException;
35+
import java.nio.charset.CodingErrorAction;
36+
import java.nio.charset.MalformedInputException;
37+
import java.nio.charset.StandardCharsets;
38+
import java.util.stream.IntStream;
39+
40+
import org.testng.annotations.DataProvider;
41+
import org.testng.annotations.Test;
42+
import static org.testng.Assert.assertEquals;
43+
import static org.testng.Assert.assertThrows;
44+
45+
@Test
46+
public class StatefulDecoderNearEOF {
47+
48+
@DataProvider
49+
public Object[][] inputs() {
50+
return new Object[][] {
51+
// BOM, followed by High surrogate (in UTF-16LE).
52+
// First read() should throw an exception.
53+
{new byte[] {(byte)0xff, (byte)0xfe, 0, (byte)0xd8}, 0},
54+
55+
// BOM, followed by 'A', 'B', 'C', then by High surrogate (in UTF-16LE).
56+
// Fourth read() should throw an exception.
57+
{new byte[] {(byte)0xff, (byte)0xfe, (byte)0x41, 0, (byte)0x42, 0, (byte)0x43, 0, 0, (byte)0xd8}, 3},
58+
};
59+
}
60+
61+
@Test (dataProvider = "inputs")
62+
public void testStatefulDecoderNearEOF(byte[] ba, int numSucessReads) throws IOException {
63+
try (var r = new InputStreamReader(
64+
new ByteArrayInputStream(ba),
65+
StandardCharsets.UTF_16.newDecoder().onMalformedInput(CodingErrorAction.REPORT))) {
66+
// Issue read() as many as numSucessReads which should not fail
67+
IntStream.rangeClosed(1, numSucessReads).forEach(i -> {
68+
try {
69+
assertEquals(r.read(), (int)ba[i * 2]);
70+
} catch (IOException e) {
71+
throw new UncheckedIOException(e);
72+
}
73+
});
74+
75+
// Final dangling high surrogate should throw an exception
76+
assertThrows(MalformedInputException.class, () -> r.read());
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)
Please sign in to comment.