Skip to content

Commit 173748c

Browse files
committed
Fix #556
1 parent e473e99 commit 173748c

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ JSON library.
1818

1919
#540: UTF8StreamJsonParser: fix byte to int conversion for malformed escapes
2020
(Alex R)
21+
#556: 'IndexOutOfBoundsException' in UTF8JsonGenerator.writeString(Reader, len)
22+
when using a negative length
23+
(reported by jacob-alan-ward@github)
2124

2225
2.9.9 (16-May-2019)
2326

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,13 @@ public void writeString(Reader reader, int len) throws IOException {
475475

476476
final char[] buf = _charBuffer;
477477

478-
//Add leading quote
479-
if ((_outputTail + len) >= _outputEnd) {
478+
// Add leading quote
479+
if (_outputTail >= _outputEnd) {
480480
_flushBuffer();
481481
}
482482
_outputBuffer[_outputTail++] = _quoteChar;
483483

484-
//read
484+
// read
485485
while (toRead > 0){
486486
int toReadNow = Math.min(toRead, buf.length);
487487
int numRead = reader.read(buf, 0, toReadNow);
@@ -496,8 +496,8 @@ public void writeString(Reader reader, int len) throws IOException {
496496
toRead -= numRead;
497497
}
498498

499-
//Add trailing quote
500-
if ((_outputTail + len) >= _outputEnd) {
499+
// Add trailing quote
500+
if (_outputTail >= _outputEnd) {
501501
_flushBuffer();
502502
}
503503
_outputBuffer[_outputTail++] = _quoteChar;

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

+29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.fasterxml.jackson.core.*;
44

5+
import java.io.ByteArrayInputStream;
56
import java.io.ByteArrayOutputStream;
7+
import java.io.InputStreamReader;
68
import java.io.StringReader;
79
import java.io.StringWriter;
810
import java.util.Random;
@@ -274,4 +276,31 @@ private void _testLongerRandomMulti(int readMode, String text, int round)
274276
assertEquals(JsonToken.END_ARRAY, p.currentToken());
275277
p.close();
276278
}
279+
280+
// [jackson-core#556]
281+
public void testIssue556() throws Exception
282+
{
283+
StringBuilder sb = new StringBuilder(8000);
284+
sb.append('"');
285+
for (int i = 0; i < 7988; i++) {
286+
sb.append("a");
287+
}
288+
sb.append('"');
289+
byte[] bytes = sb.toString().getBytes("UTF-8");
290+
291+
StringWriter w = new StringWriter(9000);
292+
JsonGenerator g = FACTORY.createGenerator(new ByteArrayOutputStream());
293+
294+
g.writeStartArray();
295+
_write556(g, sb.toString());
296+
_write556(g, "b");
297+
_write556(g, "c");
298+
g.writeEndArray();
299+
g.close();
300+
}
301+
302+
private void _write556(JsonGenerator g, String value) throws Exception
303+
{
304+
g.writeString(new StringReader(value), -1);
305+
}
277306
}

0 commit comments

Comments
 (0)