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

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

commit cea1f802d0d3d212fb0a528655e3b30db7291970
Author: Benoit TELLIER <[email protected]>
AuthorDate: Thu Nov 5 05:25:51 2026 +0100

    [FIX] LineReaderInputStreamAdaptor: skip(n) never skips more than n
    
    skip() asked for the whole buffer on every read. After a short read, for
    example at the edge of the parser's buffer or at the limit of a boundary
    stream, what was left to skip was smaller than the buffer, so skip(n)
    consumed and returned more than n: up to 8191 extra bytes were dropped.
    Each read now asks for what is left to skip.
---
 .../mime4j/io/LineReaderInputStreamAdaptor.java    |  2 +-
 .../io/LineReaderInputStreamAdaptorTest.java       | 24 ++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java
 
b/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java
index 1cd9e2e1..caf53f1c 100644
--- 
a/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java
+++ 
b/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java
@@ -145,7 +145,7 @@ public class LineReaderInputStreamAdaptor extends 
LineReaderInputStream {
         final byte[] buffer = new byte[bufferSize];
         long result = 0;
         while (count > 0) {
-            int res = read(buffer);
+            int res = read(buffer, 0, (int) Math.min(buffer.length, count));
             if (res == -1) {
                 break;
             }
diff --git 
a/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java
 
b/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java
index ef48af7b..40cf9b62 100644
--- 
a/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java
+++ 
b/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java
@@ -24,6 +24,11 @@ import org.apache.james.mime4j.util.ContentUtil;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.io.ByteArrayInputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
 public class LineReaderInputStreamAdaptorTest {
 
     private static LineReaderInputStreamAdaptor create(final String s) {
@@ -182,4 +187,23 @@ public class LineReaderInputStreamAdaptorTest {
         instream2.close();
     }
 
+
+    @Test
+    public void skipShouldNotGoBeyondTheRequestedCountWhenReadsAreShort() 
throws Exception {
+        byte[] content = new byte[100];
+        for (int i = 0; i < content.length; i++) {
+            content[i] = (byte) i;
+        }
+        // Stands for the parser's buffer or MimeBoundaryInputStream: at most 
7 bytes per read
+        InputStream shortReads = new FilterInputStream(new 
ByteArrayInputStream(content)) {
+            @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                return super.read(b, off, Math.min(len, 7));
+            }
+        };
+        LineReaderInputStreamAdaptor stream = new 
LineReaderInputStreamAdaptor(shortReads);
+
+        Assert.assertEquals(10, stream.skip(10));
+        Assert.assertEquals(10, stream.read());
+    }
 }


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

Reply via email to