Skip to content

Commit 09c18b1

Browse files
committedSep 9, 2019
Fix #178
1 parent 47daf9c commit 09c18b1

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed
 

‎cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -3052,13 +3052,14 @@ protected final void _loadToHaveAtLeast(int minAvailable) throws IOException
30523052
// Need to move remaining data in front?
30533053
int amount = _inputEnd - _inputPtr;
30543054
if (amount > 0 && _inputPtr > 0) {
3055-
_currInputProcessed += _inputPtr;
30563055
//_currInputRowStart -= _inputPtr;
30573056
System.arraycopy(_inputBuffer, _inputPtr, _inputBuffer, 0, amount);
30583057
_inputEnd = amount;
30593058
} else {
30603059
_inputEnd = 0;
30613060
}
3061+
// Needs to be done here, as per [dataformats-binary#178]
3062+
_currInputProcessed += _inputPtr;
30623063
_inputPtr = 0;
30633064
while (_inputEnd < minAvailable) {
30643065
int count = _inputStream.read(_inputBuffer, _inputEnd, _inputBuffer.length - _inputEnd);

‎cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/CBORTestBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected byte[] cborDoc(String json) throws IOException {
5353
return cborDoc(cborFactory(), json);
5454
}
5555

56-
protected byte[] cborDoc(CBORFactory cborF, String json) throws IOException
56+
protected byte[] cborDoc(TokenStreamFactory cborF, String json) throws IOException
5757
{
5858
JsonFactory jf = new JsonFactory();
5959
JsonParser p = jf.createParser(json);

‎cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/ParserInputStreamTest.java

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package com.fasterxml.jackson.dataformat.cbor;
22

3-
import com.fasterxml.jackson.databind.JsonNode;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import org.junit.Test;
6-
73
import java.io.ByteArrayInputStream;
84
import java.io.IOException;
5+
import java.io.InputStream;
96
import java.io.SequenceInputStream;
107

11-
// for [dataformat-cbor#13]
8+
import com.fasterxml.jackson.core.JsonFactory;
9+
import com.fasterxml.jackson.core.JsonParser;
10+
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
1214
public class ParserInputStreamTest extends CBORTestBase
1315
{
14-
@Test
16+
private final ObjectMapper MAPPER = cborMapper();
17+
18+
// for [dataformat-cbor#13]
1519
public void testInpuStream() throws Exception {
16-
CBORFactory f = new CBORFactory();
17-
ObjectMapper cborMapper = new ObjectMapper(new CBORFactory());
18-
byte[] buffer = generateHugeCBOR(f);
20+
byte[] buffer = generateHugeCBOR(MAPPER.getFactory());
1921

2022
// split the buffer in two smaller buffer
2123
int len = 160;
@@ -29,11 +31,34 @@ public void testInpuStream() throws Exception {
2931
ByteArrayInputStream in2 = new ByteArrayInputStream(buf2);
3032
SequenceInputStream inputStream = new SequenceInputStream(in1, in2);
3133

32-
JsonNode jsonNode = cborMapper.readTree(inputStream);
34+
JsonNode jsonNode = MAPPER.readTree(inputStream);
3335
assertNotNull(jsonNode);
3436
}
3537

36-
private byte[] generateHugeCBOR(CBORFactory f) throws IOException {
38+
public void testInputStreamWithHugeValueThatOverlaps() throws Exception {
39+
final byte[] buffer = new byte[8002];
40+
buffer[0] = 0x79; // string length 7996 + 3 init bytes
41+
buffer[1] = 0x1f;
42+
buffer[2] = 0x3c;
43+
buffer[7999] = 0x61; // string length 1 + 1 init byte
44+
45+
final InputStream in = new ByteArrayInputStream(buffer);
46+
final JsonParser parser = MAPPER.getFactory().createParser(in);
47+
48+
parser.nextToken();
49+
parser.finishToken();
50+
51+
final long start = parser.getCurrentLocation().getByteOffset();
52+
assertEquals(7999, start);
53+
54+
parser.nextToken();
55+
parser.finishToken();
56+
57+
final long end = parser.getCurrentLocation().getByteOffset();
58+
assertEquals(8001, end);
59+
}
60+
61+
private byte[] generateHugeCBOR(JsonFactory f) throws IOException {
3762
String hugeJson = "{";
3863
for (char c='a'; c <= 'z'; c++) {
3964
for (char cc='a'; cc <= 'z'; cc++) {

‎release-notes/CREDITS-2.x

+3-2
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,6 @@ Marcos Passos (marcospassos@github)
105105
* Contributed fix for #173: (Avro) Improve Union type serialization performance
106106
(2.10.0)
107107

108-
109-
108+
John (iziamos@github)
109+
* Reported, suggested fix for #178: Fix issue wit input offsets when parsing CBOR from `InputStream`
110+
(2.10.0)

‎release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project: jackson-datatypes-binaryModules:
1212

1313
#148: (protobuf) Add `ProtobufMapper.generateSchemaFor(TypeReference<?>)` overload
1414
(suggested by MrThreepwood@github)
15+
#178: Fix issue wit input offsets when parsing CBOR from `InputStream`
16+
(reported by iziamos@github)
1517

1618
2.10.0.pr2 (31-Aug-2019)
1719

0 commit comments

Comments
 (0)