-
Notifications
You must be signed in to change notification settings - Fork 20
Add system property to cap memory usage in ResponseInputStream #149
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
base: master
Are you sure you want to change the base?
Conversation
Right now it will consume an unbounded amount of memory if the remote server sends a response with nonsense all on one line.
| public class ResponseInputStream { | ||
|
|
||
| private static final Integer INPUT_STREAM_BUFFER_MAX_SIZE = | ||
| Integer.parseInt(System.getProperty("org.eclipse.angus.mail.iap.inputStreamBufferMaxSize", "0")); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // 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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do an unsigned compare.
| 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.
|
@tbelaire May I ask when this MR can be merged? |
jmehrens
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Waiting on changes.
|
@baoxinggit PR has to be updated per feedback. |
jmehrens
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No new changes received.
Right now it will consume an unbounded amount of memory if the remote server sends a response with nonsense all on one line.
It could also use a fixed limit instead of making it configurable if that is preferable.