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 dac6bbdd [ENHANCEMENT] LenientDateTimeFieldTest should be more 
predicatble
dac6bbdd is described below

commit dac6bbdd9bf7464e0d2c9101cc1317ac79e6ccd5
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Nov 17 09:37:09 2026 +0100

    [ENHANCEMENT] LenientDateTimeFieldTest should be more predicatble
---
 .../mime4j/field/DateTimeFieldLenientImpl.java     | 47 ++++++++++++++++++++--
 .../mime4j/field/LenientDateTimeFieldTest.java     | 32 +++++++++++++++
 2 files changed, 75 insertions(+), 4 deletions(-)

diff --git 
a/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java 
b/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java
index 2a0255a8..ff1a3c91 100644
--- 
a/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java
+++ 
b/dom/src/main/java/org/apache/james/mime4j/field/DateTimeFieldLenientImpl.java
@@ -375,15 +375,54 @@ public class DateTimeFieldLenientImpl extends 
AbstractField implements DateTimeF
      * RFC 5322 obs-hour, obs-minute and obs-second allow CFWS around the 
colons of the time
      * (<code>09:55: 06</code>, <code>09:55:(c)06</code>), as the strict 
parser reads them. Applied
      * to a text whose white space is collapsed. Nested comments are not 
removed: the value is then
-     * not read, see {@link #zoneLeftUnread}.
+     * not read, see {@link #zoneLeftUnread}. A linear scan: a regex loop over 
the comments
+     * recursed once per comment, and a run of them overflowed the stack.
      */
-    private static final Pattern CFWS_AROUND_TIME_COLON = 
Pattern.compile("(\\d)(?: |\\([^()]*\\))*:(?: |\\([^()]*\\))*(?=\\d)");
-
     private static String removeCfwsAroundTimeColons(final String text) {
         if (text.indexOf('(') < 0 && text.indexOf(": ") < 0 && text.indexOf(" 
:") < 0) {
             return text;
         }
-        return CFWS_AROUND_TIME_COLON.matcher(text).replaceAll("$1:");
+        StringBuilder sb = new StringBuilder(text.length());
+        int i = 0;
+        while (i < text.length()) {
+            char c = text.charAt(i++);
+            sb.append(c);
+            if (c >= '0' && c <= '9') {
+                int colon = skipCfws(text, i);
+                if (colon < text.length() && text.charAt(colon) == ':') {
+                    int next = skipCfws(text, colon + 1);
+                    if (next < text.length() && text.charAt(next) >= '0' && 
text.charAt(next) <= '9') {
+                        sb.append(':');
+                        i = next;
+                    }
+                }
+            }
+        }
+        return sb.toString();
+    }
+
+    /** Skips blanks and comments without nesting; returns the first position 
after them. */
+    private static int skipCfws(final String text, final int from) {
+        int p = from;
+        while (p < text.length()) {
+            char c = text.charAt(p);
+            if (c == ' ') {
+                p++;
+            } else if (c == '(') {
+                int close = p + 1;
+                while (close < text.length() && text.charAt(close) != '(' && 
text.charAt(close) != ')') {
+                    close++;
+                }
+                if (close >= text.length() || text.charAt(close) != ')') {
+                    // nested or unclosed comment: not removed
+                    return p;
+                }
+                p = close + 1;
+            } else {
+                return p;
+            }
+        }
+        return p;
     }
 
     /**
diff --git 
a/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java 
b/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java
index bc438a0a..7bfd966c 100644
--- 
a/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java
+++ 
b/dom/src/test/java/org/apache/james/mime4j/field/LenientDateTimeFieldTest.java
@@ -216,4 +216,36 @@ public class LenientDateTimeFieldTest {
         // a nested comment is not removed: rather no date than one without 
its seconds and zone
         Assert.assertNull(parse("Date: Fri, 21 Nov 1997 09:55:((x))06 
-0600").getDate());
     }
+    static Date dateOf(final String body) {
+        return DateTimeFieldLenientImpl.PARSER.parse(new RawField("Date", 
body), null).getDate();
+    }
+
+    @Test(timeout = 10000)
+    public void manyEmptyCommentsBeforeATimeColonGiveNoDate() {
+        // a regex loop recursed once per comment: StackOverflowError
+        StringBuilder value = new StringBuilder("1");
+        for (int i = 0; i < 32_000; i++) {
+            value.append("()");
+        }
+        Assert.assertNull(dateOf(value.append(":1").toString()));
+    }
+
+    @Test(timeout = 10000)
+    public void manyAlternatingBlanksAndCommentsBeforeATimeColonGiveNoDate() {
+        StringBuilder value = new StringBuilder("1");
+        for (int i = 0; i < 16_000; i++) {
+            value.append(" ( )");
+        }
+        Assert.assertNull(dateOf(value.append(":1").toString()));
+    }
+
+    @Test(timeout = 10000)
+    public void manyCommentsAroundATimeColonAreRemoved() {
+        StringBuilder comments = new StringBuilder();
+        for (int i = 0; i < 16_000; i++) {
+            comments.append("() ");
+        }
+        Assert.assertEquals(880127706000L,
+            dateOf("Fri, 21 Nov 1997 09:55" + comments + ":" + comments + "06 
-0600").getTime());
+    }
 }


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

Reply via email to