java
java copied to clipboard
Leaking buffer negative index fault
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.