Skip to content

Fix infinite loop in AbstractByteArrayOutputStream. #758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2025

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.
@@ -92,7 +92,7 @@ protected interface InputStreamConstructor<T extends InputStream> {
private byte[] currentBuffer;

/** 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.

@@ -147,7 +147,8 @@ protected void needNewBuffer(final int newCount) {
// Creating new buffer
final int newBufferSize;
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