Skip to content

Commit 0dce23b

Browse files
authored
Fix a out of bounds exception when writing to the outBuf (#643)
1 parent ae3f5b1 commit 0dce23b

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

cbor/src/main/java/tools/jackson/dataformat/cbor/CBORParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ private final int _finishLongTextAscii(int len) throws JacksonException
25922592
int outPtr = 0;
25932593
while (len > 0) {
25942594
// load as much input as possible
2595-
int size = Math.min(len, Math.min(outBuf.length, input.length));
2595+
int size = Math.min(len, Math.min((outBuf.length - outPtr), input.length));
25962596
if (!_tryToLoadToHaveAtLeast(size)) {
25972597
return len;
25982598
}

cbor/src/test/java/tools/jackson/dataformat/cbor/parse/ParseLongAsciiTextTest.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,30 @@ public class ParseLongAsciiTextTest extends CBORTestBase
2020
@Test
2121
public void testLongNonChunkedAsciiText() throws Exception
2222
{
23-
try (JsonParser p = CBOR_F.createParser(ObjectReadContext.empty(),
24-
this.getClass().getResourceAsStream("/data/macbeth-snippet-non-chunked.cbor"))) {
25-
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
26-
String expected = new String(readResource("/data/macbeth-snippet.txt"), "UTF-8");
27-
assertEquals(expected, p.getString());
23+
// run several times to allow the internal buffers
24+
// to grow
25+
for (int x = 0; x < 3 ; x++) {
26+
try (JsonParser p = CBOR_F.createParser(ObjectReadContext.empty(),
27+
this.getClass().getResourceAsStream("/data/macbeth-snippet-non-chunked.cbor"))) {
28+
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
29+
String expected = new String(readResource("/data/macbeth-snippet.txt"), "UTF-8");
30+
assertEquals(expected, p.getString());
31+
}
2832
}
2933
}
3034

3135
@Test
3236
public void testLongChunkedAsciiText() throws Exception
3337
{
34-
try (JsonParser p = CBOR_F.createParser(ObjectReadContext.empty(),
35-
this.getClass().getResourceAsStream("/data/macbeth-snippet-chunked.cbor"))) {
36-
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
37-
String expected = new String(readResource("/data/macbeth-snippet.txt"), StandardCharsets.UTF_8);
38-
assertEquals(expected, p.getString());
38+
// run several times to allow the internal buffers
39+
// to grow
40+
for (int x = 0; x < 3 ; x++) {
41+
try (JsonParser p = CBOR_F.createParser(ObjectReadContext.empty(),
42+
this.getClass().getResourceAsStream("/data/macbeth-snippet-chunked.cbor"))) {
43+
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
44+
String expected = new String(readResource("/data/macbeth-snippet.txt"), StandardCharsets.UTF_8);
45+
assertEquals(expected, p.getString());
46+
}
3947
}
4048
}
4149
}

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)