chibenwa commented on a change in pull request #886:
URL: https://github.com/apache/james-project/pull/886#discussion_r814558882



##########
File path: 
server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ChannelImapResponseWriter.java
##########
@@ -77,7 +77,7 @@ public void write(Literal literal) throws IOException {
                     channel.writeAndFlush(new ChunkedNioFile(fc, 8192));
                 }
             } else {
-                channel.writeAndFlush(new 
ChunkedStream(literal.getInputStream()));
+                
channel.writeAndFlush((Unpooled.wrappedBuffer(IOUtils.toByteArray(literal.getInputStream()))));

Review comment:
       No please! :-1:
   
    This can be potentaly VERY large and we will trash James Memory...
   
   I have the fix: `PartialFetchBodyElement.LimitingInputStream::available` 
should not return negative values. The ChunkedInput relies on this as an 
heuristic to know if there is still input to read. However the stream is 
wrapped into a PushBackInputStream, which do not handle well negative 
availability:
   
   ```
       public int available() throws IOException {
           this.ensureOpen();
           int n = this.buf.length - this.pos;
           int avail = super.available();
           return n > 2147483647 - avail ? 2147483647 : n + avail;
       }
   ```
   
   Note what the doc has to say about it: 
https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available()
   
   > an estimate of the number of bytes that can be read (or skipped over) from 
this input stream without blocking or 0 when it reaches the end of the input 
stream.
   
   My fix would be to rather make 
`PartialFetchBodyElement.LimitingInputStream::available` return `0` instead of 
`-1`, I tried quickly, it seems to work fine!
   
   ```
           @Override
           public int available() throws IOException {
               // Correctly calculate in available bytes.
               // See IMAP-295
               checkOffset();
               if (pos >= length) {
                   return 0;
               }
               int i = in.available();
               if (i == -1) {
                   return 0;
               } else {
                   if (i >= length) {
                       return (int) length - (int) pos;
                   } else {
                       return i;
                   }
               }
           }
   ```
   
   Can you please adopt this fix here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to