Search in sources :

Example 46 with StreamReadException

use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-dataformats-binary by FasterXML.

the class Fuzz_35979_StringValueTest method testInvalidTextValueWithBrokenUTF8.

// [dataformats-binary#316]
// https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=35979
public void testInvalidTextValueWithBrokenUTF8() throws Exception {
    final byte[] input = readResource("/data/clusterfuzz-cbor-35979.cbor");
    try (JsonParser p = MAPPER.createParser(input)) {
        assertToken(JsonToken.VALUE_STRING, p.nextToken());
        p.getText();
        fail("Should not pass");
    } catch (StreamReadException e) {
        verifyException(e, "Truncated UTF-8 character in Short Unicode Name (36 bytes)");
    }
}
Also used : StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 47 with StreamReadException

use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-dataformats-binary by FasterXML.

the class SmileParserBootstrapper method constructParser.

public SmileParser constructParser(ObjectReadContext readCtxt, int factoryFeatures, int generalParserFeatures, int smileFeatures, ByteQuadsCanonicalizer rootByteSymbols) throws JacksonException {
    // 13-Mar-2021, tatu: [dataformats-binary#252] Create canonicalizing OR
    // placeholder, depending on settings
    ByteQuadsCanonicalizer can = rootByteSymbols.makeChildOrPlaceholder(factoryFeatures);
    // We just need a single byte, really, to know if it starts with header
    int end = _inputEnd;
    if ((_inputPtr < end) && (_in != null)) {
        try {
            int count = _in.read(_inputBuffer, end, _inputBuffer.length - end);
            if (count > 0) {
                _inputEnd += count;
            }
        } catch (IOException e) {
            throw WrappedIOException.construct(e);
        }
    }
    SmileParser p = new SmileParser(readCtxt, _ioContext, generalParserFeatures, smileFeatures, can, _in, _inputBuffer, _inputPtr, _inputEnd, _bufferRecyclable);
    boolean hadSig = false;
    if (_inputPtr >= _inputEnd) {
        // JAX-RS provider, empty PUT/POST payloads.
        return p;
    }
    final byte firstByte = _inputBuffer[_inputPtr];
    if (firstByte == SmileConstants.HEADER_BYTE_1) {
        // need to ensure it gets properly handled so caller won't see the signature
        hadSig = p.handleSignature(true, true);
    }
    if (!hadSig && SmileParser.Feature.REQUIRE_HEADER.enabledIn(smileFeatures)) {
        // Ok, first, let's see if it looks like plain JSON...
        String msg;
        if (firstByte == '{' || firstByte == '[') {
            msg = "Input does not start with Smile format header (first byte = 0x" + Integer.toHexString(firstByte & 0xFF) + ") -- rather, it starts with '" + ((char) firstByte) + "' (plain JSON input?) -- can not parse";
        } else {
            msg = "Input does not start with Smile format header (first byte = 0x" + Integer.toHexString(firstByte & 0xFF) + ") and parser has REQUIRE_HEADER enabled: can not parse";
        }
        throw new StreamReadException(p, msg);
    }
    return p;
}
Also used : ByteQuadsCanonicalizer(com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException) WrappedIOException(com.fasterxml.jackson.core.exc.WrappedIOException)

Example 48 with StreamReadException

use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-dataformats-binary by FasterXML.

the class NumberParsingTest method testMixedAccessForInts.

public void testMixedAccessForInts() throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    SmileGenerator g = _smileGenerator(bo, false);
    g.writeNumber(123);
    g.writeNumber(123);
    g.writeNumber(123);
    g.writeNumber(123);
    final long LONG_VALUE = 100L + Integer.MAX_VALUE;
    g.writeNumber(LONG_VALUE);
    g.writeNumber(LONG_VALUE);
    g.writeNumber(LONG_VALUE);
    g.writeNumber(LONG_VALUE);
    g.writeNumber(LONG_VALUE);
    g.writeNumber(BigInteger.TEN);
    g.writeNumber(BigInteger.TEN);
    g.writeNumber(BigInteger.TEN);
    g.writeNumber(BigInteger.TEN);
    g.writeNumber(BigInteger.TEN);
    g.close();
    byte[] data = bo.toByteArray();
    JsonParser p = _smileParser(data);
    // for ints
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(123L, p.getLongValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(123d, p.getDoubleValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(123f, p.getFloatValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(Integer.valueOf(123), p.getNumberValue());
    // for longs
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals((double) LONG_VALUE, p.getDoubleValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals((float) LONG_VALUE, p.getFloatValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigInteger.valueOf(LONG_VALUE), p.getBigIntegerValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(Long.valueOf(LONG_VALUE), p.getNumberValue());
    // and can't represent as int so:
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    try {
        p.getIntValue();
        fail("Should not pass");
    } catch (StreamReadException e) {
        verifyException(e, "Numeric value");
        verifyException(e, "out of range of int");
    }
    // for BigIntegers
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(10, p.getIntValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(10L, p.getLongValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(10f, p.getFloatValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(10d, p.getDoubleValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigInteger.valueOf(10L), p.getNumberValue());
    p.close();
}
Also used : StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 49 with StreamReadException

use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-dataformats-binary by FasterXML.

the class AsyncReaderWrapperForByteArray method nextToken.

@Override
public JsonToken nextToken() {
    JsonToken token;
    while ((token = _streamReader.nextToken()) == JsonToken.NOT_AVAILABLE) {
        ByteArrayFeeder feeder = (ByteArrayFeeder) _streamReader.nonBlockingInputFeeder();
        if (!feeder.needMoreInput()) {
            throw new StreamReadException(null, "Got NOT_AVAILABLE, could not feed more input");
        }
        int amount = Math.min(_bytesPerFeed, _end - _offset);
        if (amount < 1) {
            // end-of-input?
            feeder.endOfInput();
        } else {
            // padding?
            if (_padding == 0) {
                feeder.feedInput(_doc, _offset, _offset + amount);
            } else {
                byte[] tmp = new byte[amount + _padding + _padding];
                System.arraycopy(_doc, _offset, tmp, _padding, amount);
                feeder.feedInput(tmp, _padding, _padding + amount);
            }
            _offset += amount;
        }
    }
    return token;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException) ByteArrayFeeder(com.fasterxml.jackson.core.async.ByteArrayFeeder)

Example 50 with StreamReadException

use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-dataformats-binary by FasterXML.

the class MapperSimpleReadTest method testStreamingFeaturesViaMapper.

/*
    /**********************************************************
    /* Tests for [dataformats-binary#301]
    /**********************************************************
     */
public void testStreamingFeaturesViaMapper() throws Exception {
    SmileMapper mapperWithHeaders = SmileMapper.builder().enable(SmileGenerator.Feature.WRITE_HEADER).enable(SmileParser.Feature.REQUIRE_HEADER).build();
    byte[] encodedWithHeader = mapperWithHeaders.writeValueAsBytes("foo");
    assertEquals(8, encodedWithHeader.length);
    SmileMapper mapperNoHeaders = SmileMapper.builder().disable(SmileGenerator.Feature.WRITE_HEADER).disable(SmileParser.Feature.REQUIRE_HEADER).build();
    byte[] encodedNoHeader = mapperNoHeaders.writeValueAsBytes("foo");
    assertEquals(4, encodedNoHeader.length);
    // And then see that we can parse; with header always
    assertEquals("foo", mapperWithHeaders.readValue(encodedWithHeader, Object.class));
    assertEquals("foo", mapperNoHeaders.readValue(encodedWithHeader, Object.class));
    // without if not required
    assertEquals("foo", mapperNoHeaders.readValue(encodedNoHeader, Object.class));
    // But the reverse will fail
    try {
        mapperWithHeaders.readValue(encodedNoHeader, Object.class);
        fail("Should not pass");
    } catch (StreamReadException e) {
        verifyException(e, "Input does not start with Smile format header");
    }
}
Also used : SmileMapper(com.fasterxml.jackson.dataformat.smile.databind.SmileMapper) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException)

Aggregations

StreamReadException (com.fasterxml.jackson.core.exc.StreamReadException)68 AsyncReaderWrapper (com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)23 JsonParser (com.fasterxml.jackson.core.JsonParser)15 JsonFactory (com.fasterxml.jackson.core.json.JsonFactory)11 CBORParser (com.fasterxml.jackson.dataformat.cbor.CBORParser)9 JsonToken (com.fasterxml.jackson.core.JsonToken)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayFeeder (com.fasterxml.jackson.core.async.ByteArrayFeeder)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URL (java.net.URL)2 Test (org.junit.Test)2 InputCoercionException (com.fasterxml.jackson.core.exc.InputCoercionException)1 WrappedIOException (com.fasterxml.jackson.core.exc.WrappedIOException)1 FilteringParserDelegate (com.fasterxml.jackson.core.filter.FilteringParserDelegate)1 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 SmileGenerator (com.fasterxml.jackson.dataformat.smile.SmileGenerator)1 SmileMapper (com.fasterxml.jackson.dataformat.smile.databind.SmileMapper)1 ThrottledInputStream (com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream)1