Skip to content

Commit

Permalink
Fix #702
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 25, 2021
1 parent 3c386bf commit 8c038fb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,7 @@ Jendrik Johannes (jjohannes@github)
Jonathan Haber (jhaber@github)
* Contributed #573: More customizable TokenFilter inclusion (using `Tokenfilter.Inclusion`)
(2.12.0)

Jeffrey Ye (jeffreye@github)
* Reported #702: `ArrayOutOfBoundException` at `WriterBasedJsonGenerator.writeString(Reader, int)`
(2.12.4)
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ JSON library.
=== Releases ===
------------------------------------------------------------------------

2.12.4 (not yet released)

#702: `ArrayOutOfBoundException` at `WriterBasedJsonGenerator.writeString(Reader, int)`
(reported by Jeffrey Y)

2.12.3 (12-Apr-2021)
2.12.2 (03-Mar-2021)
2.12.1 (08-Jan-2021)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,37 +430,32 @@ public void writeString(String text) throws IOException
}

@Override
public void writeString(Reader reader, int len) throws IOException {
public void writeString(Reader reader, final int len) throws IOException
{
_verifyValueWrite(WRITE_STRING);
if (reader == null) {
_reportError("null reader");
return; // just to block warnings by lgtm.com
}
int toRead = (len >= 0) ? len : Integer.MAX_VALUE;
//Add leading quote
if ((_outputTail + len) >= _outputEnd) {
// Add leading quote
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;

final char[] buf = _allocateCopyBuffer();
//read
while (toRead > 0) {
int toReadNow = Math.min(toRead, buf.length);
int numRead = reader.read(buf, 0, toReadNow);
if (numRead <= 0) {
break;
}
if ((_outputTail + len) >= _outputEnd) {
_flushBuffer();
}
_writeString(buf, 0, numRead);

//decrease tracker
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 @@ -30,28 +30,29 @@ public void testBasicEscaping() throws Exception
// for [core#194]
public void testMediumStringsBytes() throws Exception
{
final JsonFactory jsonF = new JsonFactory();
for (int mode : ALL_BINARY_MODES) {
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000 }) {
_testMediumStrings(mode, size);
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000, 33333 }) {
_testMediumStrings(jsonF, mode, size);
}
}
}

// for [core#194]
public void testMediumStringsChars() throws Exception
{
final JsonFactory jsonF = new JsonFactory();
for (int mode : ALL_TEXT_MODES) {
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000 }) {
_testMediumStrings(mode, size);
for (int size : new int[] { 1100, 2300, 3800, 7500, 19000, 33333 }) {
_testMediumStrings(jsonF, mode, size);
}
}
}

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

public void testLongerRandomMultiChunk() throws Exception
{
/* Let's first generate 100k of pseudo-random characters, favoring
* 7-bit ascii range
*/
// Let's first generate 100k of pseudo-random characters, favoring
// 7-bit ascii range
for (int mode : ALL_TEXT_MODES) {
for (int round = 0; round < 70; ++round) {
String content = generateRandom(73000+round);
Expand Down Expand Up @@ -126,14 +126,15 @@ private String generateRandom(int len)
return sb.toString();
}

private void _testMediumStrings(int readMode, int length) throws Exception
private void _testMediumStrings(JsonFactory jsonF,
int readMode, int length) throws Exception
{
String text = _generareMediumText(length);
StringWriter sw = new StringWriter();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();

JsonGenerator gen = (readMode != MODE_READER) ? FACTORY.createGenerator(bytes)
: FACTORY.createGenerator(sw);
JsonGenerator gen = (readMode != MODE_READER) ? jsonF.createGenerator(bytes)
: jsonF.createGenerator(sw);
gen.writeStartArray();

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

JsonParser p;
if (readMode == MODE_READER) {
p = FACTORY.createParser(sw.toString());
p = jsonF.createParser(sw.toString());
} else {
p = createParser(FACTORY, readMode, bytes.toByteArray());
p = createParser(jsonF, readMode, bytes.toByteArray());
}
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(text, p.getText());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}

private void doTestBasicEscaping() throws Exception
{
for (int i = 0; i < SAMPLES.length; ++i) {
Expand Down

0 comments on commit 8c038fb

Please sign in to comment.