-
-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #834 (rare buffer condition for number-parsing, found by oss-fuzz…
… project)
- Loading branch information
1 parent
2e2cff0
commit 0d48020
Showing
5 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/test/java/com/fasterxml/jackson/core/fuzz/Fuzz52688ParseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.fasterxml.jackson.core.fuzz; | ||
|
||
import java.io.*; | ||
import java.math.BigInteger; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
import com.fasterxml.jackson.core.exc.StreamReadException; | ||
import com.fasterxml.jackson.core.testsupport.ThrottledInputStream; | ||
|
||
// Reproducing: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=52688 | ||
// (reported as [core#834] | ||
public class Fuzz52688ParseTest extends BaseTest | ||
{ | ||
private final JsonFactory JSON_F = new JsonFactory(); | ||
|
||
private final static BigInteger BIG_NUMBER = new BigInteger("3222" | ||
+"2222" | ||
+"2222" | ||
+"2222" | ||
+"222"); | ||
|
||
public void testBigNumberUTF16Parse() throws Exception | ||
{ | ||
// 41 bytes as UTF16-LE; becomes 21 characters (last broken) | ||
final byte[] DOC = { | ||
0x33, 0, 0x32, 0, 0x32, 0, 0x32, 0, | ||
0x32, 0, 0x32, 0, 0x32, 0, 0x32, 0, | ||
0x32, 0, 0x32, 0, 0x32, 0, 0x32, 0, | ||
0x32, 0, 0x32, 0, 0x32, 0, 0x32, 0, | ||
0x32, 0, 0x32, 0, 0x32, 0, 0xd, 0, | ||
0x32 | ||
}; | ||
|
||
try (JsonParser p = JSON_F.createParser(/*ObjectReadContext.empty(), */ | ||
new ByteArrayInputStream(DOC))) { | ||
assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); | ||
assertEquals(BIG_NUMBER, p.getBigIntegerValue()); | ||
assertEquals(1, p.currentLocation().getLineNr()); | ||
|
||
// and now we should fail for the weird character | ||
try { | ||
JsonToken t = p.nextToken(); | ||
fail("Should not pass, next token = "+t); | ||
} catch (StreamReadException e) { | ||
verifyException(e, "Unexpected character"); | ||
assertEquals(2, p.currentLocation().getLineNr()); | ||
assertEquals(2, e.getLocation().getLineNr()); | ||
} | ||
} | ||
} | ||
|
||
public void testBigNumberUTF8Parse() throws Exception | ||
{ | ||
// Similar to UTF-16 case | ||
final byte[] DOC = { | ||
0x33, 0x32, 0x32, 0x32, | ||
0x32, 0x32, 0x32, 0x32, | ||
0x32, 0x32, 0x32, 0x32, | ||
0x32, 0x32, 0x32, 0x32, | ||
0x32, 0x32, 0x32, 0xd, | ||
(byte) '@' | ||
}; | ||
|
||
// Try to force buffer condition | ||
try (ThrottledInputStream in = new ThrottledInputStream(DOC, 1)) { | ||
try (JsonParser p = JSON_F.createParser(/*ObjectReadContext.empty(), */ in)) { | ||
assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); | ||
assertEquals(BIG_NUMBER, p.getBigIntegerValue()); | ||
assertEquals(1, p.currentLocation().getLineNr()); | ||
|
||
// and now we should fail for the weird character | ||
try { | ||
JsonToken t = p.nextToken(); | ||
fail("Should not pass, next token = "+t); | ||
} catch (StreamReadException e) { | ||
verifyException(e, "Unexpected character"); | ||
assertEquals(2, p.currentLocation().getLineNr()); | ||
assertEquals(2, e.getLocation().getLineNr()); | ||
} | ||
} | ||
} | ||
} | ||
} |