|
| 1 | +package com.fasterxml.jackson.core.fuzz; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.math.BigInteger; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.*; |
| 7 | +import com.fasterxml.jackson.core.exc.StreamReadException; |
| 8 | +import com.fasterxml.jackson.core.testsupport.ThrottledInputStream; |
| 9 | + |
| 10 | +// Reproducing: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=52688 |
| 11 | +// (reported as [core#834] |
| 12 | +public class Fuzz52688ParseTest extends BaseTest |
| 13 | +{ |
| 14 | + private final JsonFactory JSON_F = new JsonFactory(); |
| 15 | + |
| 16 | + private final static BigInteger BIG_NUMBER = new BigInteger("3222" |
| 17 | + +"2222" |
| 18 | + +"2222" |
| 19 | + +"2222" |
| 20 | + +"222"); |
| 21 | + |
| 22 | + public void testBigNumberUTF16Parse() throws Exception |
| 23 | + { |
| 24 | + // 41 bytes as UTF16-LE; becomes 21 characters (last broken) |
| 25 | + final byte[] DOC = { |
| 26 | + 0x33, 0, 0x32, 0, 0x32, 0, 0x32, 0, |
| 27 | + 0x32, 0, 0x32, 0, 0x32, 0, 0x32, 0, |
| 28 | + 0x32, 0, 0x32, 0, 0x32, 0, 0x32, 0, |
| 29 | + 0x32, 0, 0x32, 0, 0x32, 0, 0x32, 0, |
| 30 | + 0x32, 0, 0x32, 0, 0x32, 0, 0xd, 0, |
| 31 | + 0x32 |
| 32 | + }; |
| 33 | + |
| 34 | + try (JsonParser p = JSON_F.createParser(/*ObjectReadContext.empty(), */ |
| 35 | + new ByteArrayInputStream(DOC))) { |
| 36 | + assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); |
| 37 | + assertEquals(BIG_NUMBER, p.getBigIntegerValue()); |
| 38 | + assertEquals(1, p.currentLocation().getLineNr()); |
| 39 | + |
| 40 | + // and now we should fail for the weird character |
| 41 | + try { |
| 42 | + JsonToken t = p.nextToken(); |
| 43 | + fail("Should not pass, next token = "+t); |
| 44 | + } catch (StreamReadException e) { |
| 45 | + verifyException(e, "Unexpected character"); |
| 46 | + assertEquals(2, p.currentLocation().getLineNr()); |
| 47 | + assertEquals(2, e.getLocation().getLineNr()); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public void testBigNumberUTF8Parse() throws Exception |
| 53 | + { |
| 54 | + // Similar to UTF-16 case |
| 55 | + final byte[] DOC = { |
| 56 | + 0x33, 0x32, 0x32, 0x32, |
| 57 | + 0x32, 0x32, 0x32, 0x32, |
| 58 | + 0x32, 0x32, 0x32, 0x32, |
| 59 | + 0x32, 0x32, 0x32, 0x32, |
| 60 | + 0x32, 0x32, 0x32, 0xd, |
| 61 | + (byte) '@' |
| 62 | + }; |
| 63 | + |
| 64 | + // Try to force buffer condition |
| 65 | + try (ThrottledInputStream in = new ThrottledInputStream(DOC, 1)) { |
| 66 | + try (JsonParser p = JSON_F.createParser(/*ObjectReadContext.empty(), */ in)) { |
| 67 | + assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); |
| 68 | + assertEquals(BIG_NUMBER, p.getBigIntegerValue()); |
| 69 | + assertEquals(1, p.currentLocation().getLineNr()); |
| 70 | + |
| 71 | + // and now we should fail for the weird character |
| 72 | + try { |
| 73 | + JsonToken t = p.nextToken(); |
| 74 | + fail("Should not pass, next token = "+t); |
| 75 | + } catch (StreamReadException e) { |
| 76 | + verifyException(e, "Unexpected character"); |
| 77 | + assertEquals(2, p.currentLocation().getLineNr()); |
| 78 | + assertEquals(2, e.getLocation().getLineNr()); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments