json-iterator / java

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
http://jsoniter.com/
MIT License
1.51k stars 518 forks source link

Leaking buffer negative index fault #241

Open svobol13 opened 5 years ago

svobol13 commented 5 years ago

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 https://github.com/json-iterator/java/issues/124. Using version 0.9.19.