Skip to content

Commit 8c038fb

Browse files
committed
Fix #702
1 parent 3c386bf commit 8c038fb

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,7 @@ Jendrik Johannes (jjohannes@github)
257257
Jonathan Haber (jhaber@github)
258258
* Contributed #573: More customizable TokenFilter inclusion (using `Tokenfilter.Inclusion`)
259259
(2.12.0)
260+
261+
Jeffrey Ye (jeffreye@github)
262+
* Reported #702: `ArrayOutOfBoundException` at `WriterBasedJsonGenerator.writeString(Reader, int)`
263+
(2.12.4)

release-notes/VERSION-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ JSON library.
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17+
2.12.4 (not yet released)
18+
19+
#702: `ArrayOutOfBoundException` at `WriterBasedJsonGenerator.writeString(Reader, int)`
20+
(reported by Jeffrey Y)
21+
1722
2.12.3 (12-Apr-2021)
1823
2.12.2 (03-Mar-2021)
1924
2.12.1 (08-Jan-2021)

src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -430,37 +430,32 @@ public void writeString(String text) throws IOException
430430
}
431431

432432
@Override
433-
public void writeString(Reader reader, int len) throws IOException {
433+
public void writeString(Reader reader, final int len) throws IOException
434+
{
434435
_verifyValueWrite(WRITE_STRING);
435436
if (reader == null) {
436437
_reportError("null reader");
437438
return; // just to block warnings by lgtm.com
438439
}
439440
int toRead = (len >= 0) ? len : Integer.MAX_VALUE;
440-
//Add leading quote
441-
if ((_outputTail + len) >= _outputEnd) {
441+
// Add leading quote
442+
if (_outputTail >= _outputEnd) {
442443
_flushBuffer();
443444
}
444445
_outputBuffer[_outputTail++] = _quoteChar;
445446

446447
final char[] buf = _allocateCopyBuffer();
447-
//read
448448
while (toRead > 0) {
449449
int toReadNow = Math.min(toRead, buf.length);
450450
int numRead = reader.read(buf, 0, toReadNow);
451451
if (numRead <= 0) {
452452
break;
453453
}
454-
if ((_outputTail + len) >= _outputEnd) {
455-
_flushBuffer();
456-
}
457454
_writeString(buf, 0, numRead);
458-
459-
//decrease tracker
460455
toRead -= numRead;
461456
}
462-
//Add trailing quote
463-
if ((_outputTail + len) >= _outputEnd) {
457+
// Add trailing quote
458+
if (_outputTail >= _outputEnd) {
464459
_flushBuffer();
465460
}
466461
_outputBuffer[_outputTail++] = _quoteChar;

src/test/java/com/fasterxml/jackson/core/json/StringGenerationFromReaderTest.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,29 @@ public void testBasicEscaping() throws Exception
3030
// for [core#194]
3131
public void testMediumStringsBytes() throws Exception
3232
{
33+
final JsonFactory jsonF = new JsonFactory();
3334
for (int mode : ALL_BINARY_MODES) {
34-
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000 }) {
35-
_testMediumStrings(mode, size);
35+
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000, 33333 }) {
36+
_testMediumStrings(jsonF, mode, size);
3637
}
3738
}
3839
}
3940

4041
// for [core#194]
4142
public void testMediumStringsChars() throws Exception
4243
{
44+
final JsonFactory jsonF = new JsonFactory();
4345
for (int mode : ALL_TEXT_MODES) {
44-
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000 }) {
45-
_testMediumStrings(mode, size);
46+
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000, 33333 }) {
47+
_testMediumStrings(jsonF, mode, size);
4648
}
4749
}
4850
}
4951

5052
public void testLongerRandomSingleChunk() throws Exception
5153
{
52-
/* Let's first generate 100k of pseudo-random characters, favoring
53-
* 7-bit ascii range
54-
*/
54+
// Let's first generate 100k of pseudo-random characters, favoring
55+
// 7-bit ascii range
5556
for (int mode : ALL_TEXT_MODES) {
5657
for (int round = 0; round < 80; ++round) {
5758
String content = generateRandom(75000+round);
@@ -62,9 +63,8 @@ public void testLongerRandomSingleChunk() throws Exception
6263

6364
public void testLongerRandomMultiChunk() throws Exception
6465
{
65-
/* Let's first generate 100k of pseudo-random characters, favoring
66-
* 7-bit ascii range
67-
*/
66+
// Let's first generate 100k of pseudo-random characters, favoring
67+
// 7-bit ascii range
6868
for (int mode : ALL_TEXT_MODES) {
6969
for (int round = 0; round < 70; ++round) {
7070
String content = generateRandom(73000+round);
@@ -126,14 +126,15 @@ private String generateRandom(int len)
126126
return sb.toString();
127127
}
128128

129-
private void _testMediumStrings(int readMode, int length) throws Exception
129+
private void _testMediumStrings(JsonFactory jsonF,
130+
int readMode, int length) throws Exception
130131
{
131132
String text = _generareMediumText(length);
132133
StringWriter sw = new StringWriter();
133134
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
134135

135-
JsonGenerator gen = (readMode != MODE_READER) ? FACTORY.createGenerator(bytes)
136-
: FACTORY.createGenerator(sw);
136+
JsonGenerator gen = (readMode != MODE_READER) ? jsonF.createGenerator(bytes)
137+
: jsonF.createGenerator(sw);
137138
gen.writeStartArray();
138139

139140
StringReader reader = new StringReader(text);
@@ -143,17 +144,17 @@ private void _testMediumStrings(int readMode, int length) throws Exception
143144

144145
JsonParser p;
145146
if (readMode == MODE_READER) {
146-
p = FACTORY.createParser(sw.toString());
147+
p = jsonF.createParser(sw.toString());
147148
} else {
148-
p = createParser(FACTORY, readMode, bytes.toByteArray());
149+
p = createParser(jsonF, readMode, bytes.toByteArray());
149150
}
150151
assertToken(JsonToken.START_ARRAY, p.nextToken());
151152
assertToken(JsonToken.VALUE_STRING, p.nextToken());
152153
assertEquals(text, p.getText());
153154
assertToken(JsonToken.END_ARRAY, p.nextToken());
154155
p.close();
155156
}
156-
157+
157158
private void doTestBasicEscaping() throws Exception
158159
{
159160
for (int i = 0; i < SAMPLES.length; ++i) {

0 commit comments

Comments
 (0)