Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit 4c371db

Browse files
committed
Merge pull request #13 from stevegury/master
Bug in boundary checking in the CBORParser
2 parents 90695a1 + ca8e405 commit 4c371db

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2265,11 +2265,11 @@ private final String _findDecodedFromSymbols(final int len) throws IOException
22652265
int inPtr = _inputPtr;
22662266
final byte[] inBuf = _inputBuffer;
22672267
int q = inBuf[inPtr] & 0xFF;
2268-
if (len > 0) {
2268+
if (len > 1) {
22692269
q = (q << 8) + (inBuf[++inPtr] & 0xFF);
2270-
if (len > 1) {
2270+
if (len > 2) {
22712271
q = (q << 8) + (inBuf[++inPtr] & 0xFF);
2272-
if (len > 2) {
2272+
if (len > 3) {
22732273
q = (q << 8) + (inBuf[++inPtr] & 0xFF);
22742274
}
22752275
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fasterxml.jackson.dataformat.cbor;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.Test;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.SequenceInputStream;
10+
11+
public class ParserInputStreamTest extends CBORTestBase {
12+
13+
@Test
14+
public void testInpuStream() throws Exception {
15+
CBORFactory f = new CBORFactory();
16+
ObjectMapper cborMapper = new ObjectMapper(new CBORFactory());
17+
byte[] buffer = generateHugeCBOR(f);
18+
19+
// split the buffer in two smaller buffer
20+
int len = 160;
21+
byte[] buf1 = new byte[len];
22+
byte[] buf2 = new byte[buffer.length - len];
23+
System.arraycopy(buffer, 0, buf1, 0, len);
24+
System.arraycopy(buffer, len, buf2, 0, buffer.length - len);
25+
26+
// aggregate the two buffers via a SequenceInputStream
27+
ByteArrayInputStream in1 = new ByteArrayInputStream(buf1);
28+
ByteArrayInputStream in2 = new ByteArrayInputStream(buf2);
29+
SequenceInputStream inputStream = new SequenceInputStream(in1, in2);
30+
31+
try {
32+
JsonNode jsonNode = cborMapper.readTree(inputStream);
33+
}
34+
catch (ArrayIndexOutOfBoundsException ex){
35+
ex.printStackTrace();
36+
fail("Shouldn't throw an ArrayIndexOutOfBoundsException while parsing!");
37+
}
38+
}
39+
40+
private byte[] generateHugeCBOR(CBORFactory f) throws IOException {
41+
String hugeJson = "{";
42+
for (char c='a'; c <= 'z'; c++) {
43+
for (char cc='a'; cc <= 'z'; cc++) {
44+
hugeJson += "\"" + c + cc + "\":0,";
45+
}
46+
for (int i = 0; i < 50; i++) {
47+
hugeJson += "\"" + c + i + "\":" + i + ",";
48+
}
49+
}
50+
hugeJson += "\"name\":123";
51+
hugeJson += "}";
52+
return cborDoc(f, hugeJson);
53+
}
54+
}

0 commit comments

Comments
 (0)