Skip to content

Leaking buffer negative index fault #241

@svobol13

Description

@svobol13

I found that when my source is "slow", buffer grows infinitely till it throws java.lang.NegativeArraySizeException. Spent quite some time trying to find minimal reproducible example (it was failing on my 8GB json I didnt really want to post here :D).

  @Test
  public void leakTest() throws IOException {
    InputStream slowStream = new InputStream() {
      int position = 0;
      boolean pretendEmptyNextRead = false;
      byte[] src = "{\"a\":\"123456789abcdef\"}".getBytes();

      @Override
      public int read() throws IOException {
        if (position < src.length) {
          if (pretendEmptyNextRead) {
            pretendEmptyNextRead = false;
            return -1;
          } else {
            pretendEmptyNextRead = true;
            return src[position++];
          }
        }
        return -1;
      }
    };

    JsonIterator jsonIterator = JsonIterator.parse(slowStream, 1024 * 1024);
    jsonIterator.readObject();
    jsonIterator.readAny();
  }

If you run this you will get the exception before parsing whole string. If I observe byte[] newBuf = new byte[iter.buf.length * 2]; it growths even if it doesnt need to.

This issue is probably duplicate of this #124. Using version 0.9.19.

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