Skip to content

Non-blocking parser reports incorrect locations when fed with non-zero offset #531

@dnault

Description

@dnault

When feeding a non-blocking parser, the input array offset leaks into the offsets reported by getCurrentLocation() and getTokenLocation().

For example, feeding with an offset of 7 yields tokens whose reported locations are 7 greater than they should be. Likewise the current location reported by the parser is 7 greater than the correct location.

It's not possible for a user to work around this issue by subtracting 7 from the reported locations, because the token location may have been established by an earlier feeding with a different offset.

Jackson version: 2.9.8

Unit test:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.async.ByteArrayFeeder;
import org.junit.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;

public class FeedingOffsetTest {

  @Test
  public void inputOffsetShouldNotAffectLocations() throws Exception {
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser parser = jsonFactory.createNonBlockingByteArrayParser();
    ByteArrayFeeder feeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();

    byte[] input = "[[[".getBytes(UTF_8);

    feeder.feedInput(input, 2, 3);
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    assertEquals(1, parser.getCurrentLocation().getByteOffset()); // ACTUAL = 3
    assertEquals(1, parser.getTokenLocation().getByteOffset());   // ACTUAL = 3

    feeder.feedInput(input, 0, 1);
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    assertEquals(2, parser.getCurrentLocation().getByteOffset());
    assertEquals(2, parser.getTokenLocation().getByteOffset());
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions