Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

public class ResponseInputStream {

private static final Integer INPUT_STREAM_BUFFER_MAX_SIZE =
Integer.parseInt(System.getProperty("org.eclipse.angus.mail.iap.inputStreamBufferMaxSize", "0"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use primitive.
Use Math.max(x, 0) to clamp value.
Trap numberformatexception and use default. PropUtils has some helper methods.

@jbescos jbescos Jul 24, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be preferable to obtain that value from Session#properties instead of a System property, because System will apply to all instances of ResponseInputStream in the whole JVM.

There is only one reference to new ResponseInputStream that is happening in angus-mail/providers/imap/src/main/java/org/eclipse/angus/mail/iap/Protocol.java. It should be easy to add a new constructor in ResponseInputStream passing the Properties as an argument. In this way we can obtain that new parameter, and it opens the possibility to add more in the future.

private static final int minIncrement = 256;
private static final int maxIncrement = 256 * 1024;
private static final int incrementSlop = 16;
Expand Down Expand Up @@ -86,6 +88,9 @@ public ByteArray readResponse(ByteArray ba) throws IOException {
int incr = buffer.length;
if (incr > maxIncrement)
incr = maxIncrement;
if (INPUT_STREAM_BUFFER_MAX_SIZE > 0 && buffer.length + incr > INPUT_STREAM_BUFFER_MAX_SIZE) {
throw new IOException("Response in ResponseInputStream grows too large.");
}
ba.grow(incr);
buffer = ba.getBytes();
}
Expand Down Expand Up @@ -122,7 +127,11 @@ public ByteArray readResponse(ByteArray ba) throws IOException {
int avail = buffer.length - idx; // available space in buffer
if (count + incrementSlop > avail) {
// need count-avail more bytes
ba.grow(Math.max(minIncrement, count + incrementSlop - avail));
int amountToGrow = Math.max(minIncrement, count + incrementSlop - avail);
if (INPUT_STREAM_BUFFER_MAX_SIZE > 0 && buffer.length + amountToGrow > INPUT_STREAM_BUFFER_MAX_SIZE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do an unsigned compare.

Suggested change
if (INPUT_STREAM_BUFFER_MAX_SIZE > 0 && buffer.length + amountToGrow > INPUT_STREAM_BUFFER_MAX_SIZE) {
if((buffer.length + amountToGrow) - INPUT_STREAM_BUFFER_MAX_SIZE > 0) {

Or use Integer.compareUnsigned.

throw new IOException("Response in ResponseInputStream grows too large while parsing literal.");
}
ba.grow(amountToGrow);
buffer = ba.getBytes();
}

Expand Down