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 7b9bfed84a8267999fb53cf60e9864dffb06e6ca
Author: Benoit TELLIER <[email protected]>
AuthorDate: Wed Nov 18 22:37:20 2026 +0100

    [PERF] FromLinePatterns: match a long line in linear time
---
 .../mime4j/mboxiterator/FromLinePatterns.java      |  5 ++--
 .../mboxiterator/MboxIteratorEdgeCasesTest.java    | 27 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git 
a/mbox/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java 
b/mbox/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java
index 73486f7a..d29136e3 100644
--- 
a/mbox/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java
+++ 
b/mbox/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java
@@ -24,17 +24,18 @@ package org.apache.james.mime4j.mboxiterator;
  * It's possible that more mailer agents wrote in the same file using 
different From_ lines.
  */
 public interface FromLinePatterns {
+    // Quantifiers are possessive and the year is checked by a look-behind: a 
long line costs linear time.
 
     /**
      * Match a line like: From [email protected] Fri Sep 09 14:04:52 2011
      */
-    String DEFAULT = "^From \\S+@\\S.*\\d{4}$";
+    String DEFAULT = "^From \\S[^\\s@]*+@\\S(?=.{4}).*+(?<=\\d{4})$";
     /**
      * Matches other type of From_ line (without @):
      * From MAILER-DAEMON Wed Oct 05 21:54:09 2011
      * Thunderbird mbox content: From - Wed Apr 02 06:51:08 2014
      */
-    String DEFAULT2 = "^From \\S+.*\\d{4}$";
+    String DEFAULT2 = "^From \\S(?=.{4}).*+(?<=\\d{4})$";
 
 
 }
diff --git 
a/mbox/src/test/java/org/apache/james/mime4j/mboxiterator/MboxIteratorEdgeCasesTest.java
 
b/mbox/src/test/java/org/apache/james/mime4j/mboxiterator/MboxIteratorEdgeCasesTest.java
index 7115006d..872e1410 100644
--- 
a/mbox/src/test/java/org/apache/james/mime4j/mboxiterator/MboxIteratorEdgeCasesTest.java
+++ 
b/mbox/src/test/java/org/apache/james/mime4j/mboxiterator/MboxIteratorEdgeCasesTest.java
@@ -26,6 +26,7 @@ import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.regex.Pattern;
 
 import org.junit.Assert;
 import org.junit.Rule;
@@ -112,4 +113,30 @@ public class MboxIteratorEdgeCasesTest {
             // "does not contain From_ lines": acceptable
         }
     }
+
+    @Test(timeout = 10000)
+    public void longLineStartingWithFromShouldBeRejectedInLinearTime() throws 
IOException {
+        String line = "From " + repeat('a', 500_000) + "\n";
+        Assert.assertEquals(Arrays.asList("a\n" + line, "b\n"),
+            messages("From a@b 2011\na\n" + line + "From c@d 2012\nb\n", 2 * 
1024 * 1024));
+    }
+
+    @Test(timeout = 10000)
+    public void 
longLineOfAtSignsStartingWithFromShouldBeRejectedInLinearTime() throws 
IOException {
+        StringBuilder line = new StringBuilder("From ");
+        for (int i = 0; i < 250_000; i++) {
+            line.append("a@");
+        }
+        line.append('\n');
+        Assert.assertEquals(Arrays.asList("a\n" + line, "b\n"),
+            messages("From a@b 2011\na\n" + line + "From c@d 2012\nb\n", 2 * 
1024 * 1024));
+    }
+
+    @Test
+    public void defaultPatternsShouldNotLetTheYearOverlapTheFirstCharacter() {
+        Assert.assertFalse(Pattern.compile(FromLinePatterns.DEFAULT, 
Pattern.MULTILINE).matcher("From a@1234").find());
+        Assert.assertTrue(Pattern.compile(FromLinePatterns.DEFAULT, 
Pattern.MULTILINE).matcher("From a@b1234").find());
+        Assert.assertFalse(Pattern.compile(FromLinePatterns.DEFAULT2, 
Pattern.MULTILINE).matcher("From 1234").find());
+        Assert.assertTrue(Pattern.compile(FromLinePatterns.DEFAULT2, 
Pattern.MULTILINE).matcher("From 12345").find());
+    }
 }


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

Reply via email to