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


The following commit(s) were added to refs/heads/master by this push:
     new e75755aa [FIX] QuotedPrintableInputStream: list a bounded number of 
blanks in the "ignored blanks" warning
e75755aa is described below

commit e75755aa765453e80e576a10a5c0256240760689
Author: Benoit TELLIER <[email protected]>
AuthorDate: Wed Dec 2 08:47:45 2026 +0100

    [FIX] QuotedPrintableInputStream: list a bounded number of blanks in the 
"ignored blanks" warning
    
    A bare CR transfers itself without the pending blanks, which stay
    pending until the next significant byte. Each such CR built a warning
    listing all of them, even for the SILENT monitor, which drops it. N
    blanks followed by M bare CRs cost N x M appends, " \r" repeated n
    times about n^2 / 8: 200,000 pairs took longer than seconds and
    allocated gigabytes.
    
    The warning now lists the first 64 blanks and "..." for the others, so
    each bare CR costs a bounded amount whatever the run. The decoded
    output is unchanged: the blanks still follow the CR.
---
 .../mime4j/codec/QuotedPrintableInputStream.java   | 11 +++++-
 .../codec/QuotedPrintableInputStreamTest.java      | 46 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git 
a/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
 
b/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
index cf7626ff..64044489 100644
--- 
a/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
+++ 
b/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
@@ -47,6 +47,9 @@ public class QuotedPrintableInputStream extends InputStream {
 
     private static final int DEFAULT_BUFFER_SIZE = 1024 * 2;
 
+    // Blanks listed in the "ignored blanks" warning, at most
+    private static final int MAX_IGNORED_BLANKS_SHOWN = 64;
+
     private static final byte EQ = 0x3D;
     private static final byte CR = 0x0D;
     private static final byte LF = 0x0A;
@@ -175,8 +178,12 @@ public class QuotedPrintableInputStream extends 
InputStream {
             }
             blanks.clear();
         } else if (blanks.length() > 0 && !keepblanks) {
-            StringBuilder sb = new StringBuilder(blanks.length() * 3);
-            for (int i = 0; i < blanks.length(); i++) sb.append(" 
").append(blanks.byteAt(i));
+            // The blanks stay pending: each bare CR comes here again with all 
of them, so the
+            // message shows a bounded number of them, not the whole run each 
time
+            int shown = Math.min(blanks.length(), MAX_IGNORED_BLANKS_SHOWN);
+            StringBuilder sb = new StringBuilder(shown * 4 + 4);
+            for (int i = 0; i < shown; i++) sb.append(" 
").append(blanks.byteAt(i));
+            if (shown < blanks.length()) sb.append(" ...");
             if (monitor.warn("ignored blanks", sb.toString()))
                 throw new IOException("ignored blanks");
         }
diff --git 
a/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
 
b/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
index 14cd37d4..988624da 100644
--- 
a/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
+++ 
b/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
@@ -28,7 +28,9 @@ import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.james.mime4j.Charsets;
@@ -315,6 +317,50 @@ public class QuotedPrintableInputStreamTest {
         Assert.assertArrayEquals(encoded, decodeReadingBy(encoded, 64));
     }
 
+    private static String repeat(String text, int times) {
+        StringBuilder sb = new StringBuilder(text.length() * times);
+        for (int i = 0; i < times; i++) {
+            sb.append(text);
+        }
+        return sb.toString();
+    }
+
+    @Test(timeout = 5000)
+    public void bareCarriageReturnsAfterBlanksShouldDecodeInLinearTime() 
throws Exception {
+        // Each bare CR listed every pending blank in a warning, even for the 
SILENT monitor:
+        // about n^2 / 8 appends for " \r" repeated, terabytes of garbage for 
1 MB
+        // every byte comes out, whatever the order of the CRs and the blanks 
held back
+        String pairs = repeat(" \r", 200000) + "x";
+        Assert.assertEquals(pairs.length(),
+            readText(new 
QuotedPrintableInputStream(InputStreams.createAscii(pairs), 
DecodeMonitor.SILENT)).length());
+
+        String runs = repeat(" ", 200000) + repeat("\r", 200000) + "x";
+        Assert.assertEquals(runs.length(),
+            readText(new 
QuotedPrintableInputStream(InputStreams.createAscii(runs), 
DecodeMonitor.SILENT)).length());
+    }
+
+    @Test
+    public void ignoredBlanksWarningShouldListABoundedNumberOfBlanks() throws 
Exception {
+        final List<String> warnings = new ArrayList<>();
+        DecodeMonitor monitor = new DecodeMonitor() {
+            @Override
+            public boolean warn(String error, String dropDesc) {
+                if (error.equals("ignored blanks")) {
+                    warnings.add(dropDesc);
+                }
+                return false;
+            }
+        };
+        String encoded = repeat(" ", 1000) + "\r\rx";
+        Assert.assertEquals("\r\r" + repeat(" ", 1000) + "x",
+            readText(new 
QuotedPrintableInputStream(InputStreams.createAscii(encoded), monitor)));
+        Assert.assertEquals(Arrays.asList(repeat(" 32", 64) + " ...", repeat(" 
32", 64) + " ..."), warnings);
+
+        warnings.clear();
+        readText(new QuotedPrintableInputStream(InputStreams.createAscii(" 
\t\rx"), monitor));
+        Assert.assertEquals(Arrays.asList(" 32 9"), warnings);
+    }
+
     @Test
     public void longRunOfBlanksShouldDecodeUnchangedWhateverTheReadSize() 
throws Exception {
         byte[] encoded = blanksThenX(100000);


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

Reply via email to