Skip to content

Commit

Permalink
Fix #556
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 27, 2019
1 parent e473e99 commit 173748c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ JSON library.

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

2.9.9 (16-May-2019)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,13 @@ public void writeString(Reader reader, int len) throws IOException {

final char[] buf = _charBuffer;

//Add leading quote
if ((_outputTail + len) >= _outputEnd) {
// Add leading quote
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;

//read
// read
while (toRead > 0){
int toReadNow = Math.min(toRead, buf.length);
int numRead = reader.read(buf, 0, toReadNow);
Expand All @@ -496,8 +496,8 @@ public void writeString(Reader reader, int len) throws IOException {
toRead -= numRead;
}

//Add trailing quote
if ((_outputTail + len) >= _outputEnd) {
// Add trailing quote
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.fasterxml.jackson.core.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Random;
Expand Down Expand Up @@ -274,4 +276,31 @@ private void _testLongerRandomMulti(int readMode, String text, int round)
assertEquals(JsonToken.END_ARRAY, p.currentToken());
p.close();
}

// [jackson-core#556]
public void testIssue556() throws Exception
{
StringBuilder sb = new StringBuilder(8000);
sb.append('"');
for (int i = 0; i < 7988; i++) {
sb.append("a");
}
sb.append('"');
byte[] bytes = sb.toString().getBytes("UTF-8");

StringWriter w = new StringWriter(9000);
JsonGenerator g = FACTORY.createGenerator(new ByteArrayOutputStream());

g.writeStartArray();
_write556(g, sb.toString());
_write556(g, "b");
_write556(g, "c");
g.writeEndArray();
g.close();
}

private void _write556(JsonGenerator g, String value) throws Exception
{
g.writeString(new StringReader(value), -1);
}
}

0 comments on commit 173748c

Please sign in to comment.