Skip to content

Commit

Permalink
fix #516
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 14, 2019
1 parent bf550f3 commit 233640b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ JSON library.
2.8.11.1 (not yet released)

#460: Failing to link `ObjectCodec` with `JsonFactory` copy constructor
#516: _inputPtr off-by-one in UTF8StreamJsonParser._parseNumber2()
(reported by Henrik G)

2.8.11 (23-Dec-2017)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ private final JsonToken _parseNumber2(char[] outBuf, int outPtr, boolean negativ
_textBuffer.setCurrentLength(outPtr);
// As per #105, need separating space between root values; check here
if (_parsingContext.inRoot()) {
_verifyRootSpace(_inputBuffer[_inputPtr++] & 0xFF);
_verifyRootSpace(_inputBuffer[_inputPtr] & 0xFF);
}

// And there we have it!
Expand Down
100 changes: 99 additions & 1 deletion src/test/java/com/fasterxml/jackson/core/json/TestRootValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testSimpleWrites() throws Exception
_testSimpleWrites(true);
}

public void _testSimpleWrites(boolean useStream) throws Exception
private void _testSimpleWrites(boolean useStream) throws Exception
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
StringWriter w = new StringWriter();
Expand All @@ -102,4 +102,102 @@ public void _testSimpleWrites(boolean useStream) throws Exception
String json = useStream ? out.toString("UTF-8") : w.toString();
assertEquals("123 \"abc\" true", json);
}

// [core#516]: Off-by-one read problem
public void testRootOffsetIssue516Bytes() throws Exception
{
// InputStream that forces _parseNumber2 to be invoked.
final InputStream in = new Issue516InputStream(new byte[][] {
"1234".getBytes("UTF-8"),
"5 true".getBytes("UTF-8")
});

JsonParser parser = JSON_F.createParser(in);
assertEquals(12345, parser.nextIntValue(0));

// Fails with com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'rue': was expecting ('true', 'false' or 'null')
assertTrue(parser.nextBooleanValue());

parser.close();
in.close();
}

// [core#516]: Off-by-one read problem
public void testRootOffsetIssue516Chars() throws Exception
{
// InputStream that forces _parseNumber2 to be invoked.
final Reader in = new Issue516Reader(new char[][] {
"1234".toCharArray(), "5 true".toCharArray()
});

JsonParser parser = JSON_F.createParser(in);
assertEquals(12345, parser.nextIntValue(0));

// Fails with com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'rue': was expecting ('true', 'false' or 'null')
assertTrue(parser.nextBooleanValue());

parser.close();
in.close();
}

static class Issue516InputStream extends InputStream
{
private final byte[][] reads;
private int currentRead;

public Issue516InputStream(byte[][] reads) {
this.reads = reads;
this.currentRead = 0;
}

@Override
public int read() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (currentRead >= reads.length) {
return -1;
}
byte[] bytes = reads[currentRead++];
if (len < bytes.length) {
throw new IllegalArgumentException();
}
System.arraycopy(bytes, 0, b, off, bytes.length);
return bytes.length;
}
}

static class Issue516Reader extends Reader
{
private final char[][] reads;
private int currentRead;

public Issue516Reader(char[][] reads) {
this.reads = reads;
this.currentRead = 0;
}

@Override
public void close() { }

@Override
public int read() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public int read(char[] b, int off, int len) throws IOException {
if (currentRead >= reads.length) {
return -1;
}
char[] bytes = reads[currentRead++];
if (len < bytes.length) {
throw new IllegalArgumentException();
}
System.arraycopy(bytes, 0, b, off, bytes.length);
return bytes.length;
}
}
}

0 comments on commit 233640b

Please sign in to comment.