This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit d95f16a3f02238491bc7ceb786be886e3b61b0a9
Author: Benoit Tellier <[email protected]>
AuthorDate: Fri Feb 7 10:13:27 2020 +0700

    JAMES-3042 ImapRequestFrameDecoder should be more resilient
    
    `newCumulationBuffer` ended up throwing nullPointer exceptions when 
NEEDED_DATA
    attachment was not well positioned.
    
    We can call super method when this attachment is not here in order to be 
more resilient
    (as only the sizing of a buffer is chosen).
---
 server/protocols/protocols-imap4/pom.xml           |  5 ++
 .../imapserver/netty/ImapRequestFrameDecoder.java  | 21 ++++----
 .../netty/ImapRequestFrameDecoderTest.java         | 63 ++++++++++++++++++++++
 3 files changed, 80 insertions(+), 9 deletions(-)

diff --git a/server/protocols/protocols-imap4/pom.xml 
b/server/protocols/protocols-imap4/pom.xml
index 521e0d9..635d0d4 100644
--- a/server/protocols/protocols-imap4/pom.xml
+++ b/server/protocols/protocols-imap4/pom.xml
@@ -80,6 +80,11 @@
             <artifactId>commons-configuration2</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>jcl-over-slf4j</artifactId>
         </dependency>
diff --git 
a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
 
b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
index 7a631f3..801f6d7 100644
--- 
a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
+++ 
b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
@@ -221,19 +221,22 @@ public class ImapRequestFrameDecoder extends FrameDecoder 
implements NettyConsta
 
     @Override
     protected synchronized ChannelBuffer 
newCumulationBuffer(ChannelHandlerContext ctx, int minimumCapacity) {
-        @SuppressWarnings("unchecked")
         Map<String, Object> attachment = (Map<String, Object>) 
ctx.getAttachment();
-        int size = (Integer) attachment.get(NEEDED_DATA);
-        
-        if (inMemorySizeLimit > 0) {
-            return ChannelBuffers.dynamicBuffer(Math.min(size, 
inMemorySizeLimit), ctx.getChannel().getConfig().getBufferFactory());
-        } else {
+        Object sizeAsObject = attachment.get(NEEDED_DATA);
+        if (sizeAsObject != null) {
+            @SuppressWarnings("unchecked")
+            int size = (Integer) sizeAsObject;
+
+            if (inMemorySizeLimit > 0) {
+                return ChannelBuffers.dynamicBuffer(Math.min(size, 
inMemorySizeLimit), ctx.getChannel().getConfig().getBufferFactory());
+            } else {
 
-            if (size > 0) {
-                return ChannelBuffers.dynamicBuffer(size, 
ctx.getChannel().getConfig().getBufferFactory());
+                if (size > 0) {
+                    return ChannelBuffers.dynamicBuffer(size, 
ctx.getChannel().getConfig().getBufferFactory());
+                }
             }
-            return super.newCumulationBuffer(ctx, minimumCapacity);
         }
+        return super.newCumulationBuffer(ctx, minimumCapacity);
     }
 
 }
diff --git 
a/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoderTest.java
 
b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoderTest.java
new file mode 100644
index 0000000..1c66c83
--- /dev/null
+++ 
b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoderTest.java
@@ -0,0 +1,63 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.imapserver.netty;
+
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.james.imap.decode.ImapDecoder;
+import org.jboss.netty.buffer.ChannelBufferFactory;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelConfig;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+class ImapRequestFrameDecoderTest {
+    ImapRequestFrameDecoder testee;
+
+    @BeforeEach
+    void setUp() {
+        testee = new ImapRequestFrameDecoder(
+            mock(ImapDecoder.class),
+            12,
+            18);
+    }
+
+    @Test
+    void newCumulationBufferShouldNotThrowWhenNoAttachments() {
+        ChannelHandlerContext channelHandler = 
mock(ChannelHandlerContext.class);
+        Channel channel = mock(Channel.class);
+        ChannelConfig channelConfig = mock(ChannelConfig.class);
+
+        
when(channelConfig.getBufferFactory()).thenReturn(mock(ChannelBufferFactory.class));
+        when(channelHandler.getChannel()).thenReturn(channel);
+        when(channel.getConfig()).thenReturn(channelConfig);
+
+        when(channelHandler.getAttachment()).thenReturn(ImmutableMap.<String, 
Object>of());
+
+        assertThatCode(() -> testee.newCumulationBuffer(channelHandler, 36))
+            .doesNotThrowAnyException();
+    }
+}
\ No newline at end of file


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

Reply via email to