Skip to content

Conversation

@qwerty4030
Copy link
Contributor

Fix infinite loop in AbstractByteArrayOutputStream.
When an AbstractByteArrayOutputStream is initialized with a 0 size buffer and writeImpl(InputStream) is called an infinite loop may occur and eventually cause an OOM exception.
This is due to passing 0 as the maximum bytes to read in InputStream#read which returns 0, then a new 0 size buffer is created, and then InputStream#read is called again therefore repeating the loop forever.
Fix this infinite loop by updating needNewBuffer to use DEFAULT_SIZE as the initial buffer size instead of 0.
This will allow InputStream#read to properly populate the buffer as expected and terminate the loop when EOF is reached.
Set initial value of currentBufferIndex to -1 so it is properly initialized to 0 when needNewBuffer is called; unrelated to the infinite loop bug.

When an AbstractByteArrayOutputStream is initialized with a 0 size buffer and writeImpl(InputStream) is called an infinite loop may occur and eventually cause an OOM exception.
This is due to passing 0 as the maximum bytes to read in InputStream#read which returns 0, then a new 0 size buffer is created, and then InputStream#read is called again therefore repeating the loop forever.
Fix this infinite loop by updating needNewBuffer to use DEFAULT_SIZE as the initial buffer size instead of 0.
This will allow InputStream#read to properly populate the buffer as expected and terminate the loop when EOF is reached.
Set initial value of currentBufferIndex to -1 so it is properly initialized to 0 when needNewBuffer is called; unrelated to the infinite loop bug.

/** The index of the current buffer. */
private int currentBufferIndex;
private int currentBufferIndex = -1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to the bug but I found this value was wrong while investigating. I dont think its causing any issues because it appears to only be used after reset is called which properly sets this value.

if (currentBuffer == null) {
newBufferSize = newCount;
// prevents 0 size buffers
newBufferSize = newCount > 0 ? newCount : DEFAULT_SIZE;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplest fix I found. Another option is to not allow ANY buffers smaller than DEFAULT_SIZE. Some things I ran into trying other fixes:

  1. if the logic allows an initial 0 size buffer; the next buffer must be >0 size. Then when toBufferedInputStream is called the tests fail because .available() returns 0 (for the initial buffer) instead of the actual length of the data in the next buffer. Also seems like a waste to even call InputStream.read with a 0 size buffer.
  2. Technically there is nothing wrong with a BAOS with an initial size of 0, so we don't want to throw an exception. The docs say the buffer is increased as needed so hopefully it's fine to default it to DEFAULT_SIZE in this case.

final @TempDir Path temporaryFolder) throws IOException {
final Path emptyFile = Files.createTempFile(temporaryFolder, getClass().getSimpleName(), "-empty.txt");

try (InputStream is = Files.newInputStream(emptyFile)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting corner case if you have an empty ByteArrayInputStream and you call read with a 0 size buffer it will return EOF BUT if you have an empty FileInputStream in that same scenario it will return 0.

@garydgregory garydgregory merged commit 0accfaa into apache:master Jul 4, 2025
18 of 21 checks passed
@garydgregory
Copy link
Member

TY @qwerty4030

Good find 👍, merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants