Skip to content

Commit eed725b

Browse files
authored
Fix a out of bounds exception (#640) when writing to the outBuf (PR #642)
1 parent 4704691 commit eed725b

5 files changed

Lines changed: 27 additions & 15 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2528,7 +2528,7 @@ private final int _finishLongTextAscii(int len) throws IOException
25282528
int outPtr = 0;
25292529
while (len > 0) {
25302530
// load as much input as possible
2531-
int size = Math.min(len, Math.min(outBuf.length, input.length));
2531+
int size = Math.min(len, Math.min((outBuf.length - outPtr), input.length));
25322532
if (!_tryToLoadToHaveAtLeast(size)) {
25332533
return len;
25342534
}

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParseLongAsciiTextTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@ public class ParseLongAsciiTextTest extends CBORTestBase
1717

1818
@Test
1919
public void testLongNonChunkedAsciiText() throws Exception {
20-
try (CBORParser p = CBOR_F.createParser(this.getClass().getResourceAsStream("/data/macbeth-snippet-non-chunked.cbor"))) {
21-
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
22-
String expected = new String(readResource("/data/macbeth-snippet.txt"), "UTF-8");
23-
assertEquals(expected, p.getText());
20+
// run several times to allow the internal buffers
21+
// to grow
22+
for (int x = 0; x < 3 ; x++) {
23+
try (CBORParser p = CBOR_F.createParser(this.getClass().getResourceAsStream("/data/macbeth-snippet-non-chunked.cbor"))) {
24+
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
25+
String expected = new String(readResource("/data/macbeth-snippet.txt"), "UTF-8");
26+
assertEquals(expected, p.getText());
27+
}
2428
}
2529
}
2630

2731
@Test
2832
public void testLongChunkedAsciiText() throws Exception {
29-
try (CBORParser p = CBOR_F.createParser(this.getClass().getResourceAsStream("/data/macbeth-snippet-chunked.cbor"))) {
30-
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
31-
String expected = new String(readResource("/data/macbeth-snippet.txt"), StandardCharsets.UTF_8);
32-
assertEquals(expected, p.getText());
33+
// run several times to allow the internal buffers
34+
// to grow
35+
for (int x = 0; x < 3 ; x++) {
36+
try (CBORParser p = CBOR_F.createParser(this.getClass().getResourceAsStream("/data/macbeth-snippet-chunked.cbor"))) {
37+
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
38+
String expected = new String(readResource("/data/macbeth-snippet.txt"), StandardCharsets.UTF_8);
39+
assertEquals(expected, p.getText());
40+
}
3341
}
3442
}
3543
}

cbor/src/test/resources/data/macbeth-snippet-chunked.cbor

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
y�---
1+
y�The Tragedy of Macbeth, often shortened to Macbeth, is a tragedy by William Shakespeare, estimated to have been first performed in 1606
2+
---
23
Act I, Scene
34

45
A desert place.
@@ -117,11 +118,11 @@ First Witch. Where hast thou been, sister?
117118
Second Witch. Killing swine.
118119
Third Witch. Sister, where thou?
119120
First Witch. A sailor's wife had chestnuts in her lap,
120-
And munch'd, and munch'd, and munch'd:-
121+
And munch'd, and munch'd, any�d munch'd:-
121122
'Give me,' quoth I:
122123
'Aroint thee, witch!' the rump-fed ronyon cries.
123124
Her husband's to Aleppo gone, master o' the Tiger:
124-
Buty� in a sieve I'll thither sail,
125+
But in a sieve I'll thither sail,
125126
And, like a rat without a tail,
126127
I'll do, I'll do, and I'll do.
127128
Second Witch. I'll give thee a wind.
@@ -223,12 +224,13 @@ Nothing afeard of what thyself didst make,
223224
Strange images of death. As thick as hail
224225
Came post with post; and every one did bear
225226
Thy praises in his kingdom's great defence,
226-
And pour'd them down before him.
227+
And pour'd them down before himy
228+
f.
227229
Angus. We are sent
228230
To give thee from our royal master thanks;
229231
Only to herald thee into his sight,
230232
Not pay thee.
231-
Ross. And, for an eary �nest of a greater honour,
233+
Ross. And, for an earnest of a greater honour,
232234
He bade me, from him, call thee thane of Cawdor:
233235
In which addition, hail, most worthy thane!
234236
For it is thine.

cbor/src/test/resources/data/macbeth-snippet-non-chunked.cbor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
y)---
1+
y)�The Tragedy of Macbeth, often shortened to Macbeth, is a tragedy by William Shakespeare, estimated to have been first performed in 1606
2+
---
23
Act I, Scene
34

45
A desert place.

cbor/src/test/resources/data/macbeth-snippet.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
The Tragedy of Macbeth, often shortened to Macbeth, is a tragedy by William Shakespeare, estimated to have been first performed in 1606
12
---
23
Act I, Scene
34

0 commit comments

Comments
 (0)