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 42f08e98 Bound ParserCursor by byte length in LenientAddressParser 
CharSequence overloads (#129)
42f08e98 is described below

commit 42f08e981b646045129e2f22ae023f2085ae837e
Author: Vincenz Conrad <[email protected]>
AuthorDate: Fri Sep 11 11:48:22 2026 +0200

    Bound ParserCursor by byte length in LenientAddressParser CharSequence 
overloads (#129)
    
    CharSequence overloads in LenientAddressParser use text.length() (not 
encoded) as input length for cursor. Cursor uses raw (encoded) as input. So it 
operates on raw input (encoded) but uses text's length (not encoded). This 
creates a mismatch when inputting text with non ascii chars. The result is that 
raw.length() - text.length() bytes are cut off at the end.
    Example: parseAddressList("Grüße [email protected]") -> 
[email protected]
    The existing test LenientAddressBuilderTest::testParseMailboxNonASCII did 
not catch that because it only contains a single 2-byte non ascii char, so only 
one byte is cut off: '>' which is tolerated by the parser.
    LenientAddressParser::parseMailbox already implements this in a correct 
manner.
---
 .../mime4j/field/address/LenientAddressParser.java |  6 +++---
 .../field/address/LenientAddressBuilderTest.java   | 24 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git 
a/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java
 
b/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java
index 63edc860..b809ed14 100644
--- 
a/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java
+++ 
b/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java
@@ -261,7 +261,7 @@ public class LenientAddressParser implements AddressParser {
 
     public Group parseGroup(final CharSequence text) {
         ByteSequence raw = ContentUtil.encode(text);
-        ParserCursor cursor = new ParserCursor(0, text.length());
+        ParserCursor cursor = new ParserCursor(0, raw.length());
         return parseGroup(raw, cursor);
     }
 
@@ -304,7 +304,7 @@ public class LenientAddressParser implements AddressParser {
 
     public Address parseAddress(final CharSequence text) {
         ByteSequence raw = ContentUtil.encode(text);
-        ParserCursor cursor = new ParserCursor(0, text.length());
+        ParserCursor cursor = new ParserCursor(0, raw.length());
         return parseAddress(raw, cursor, null);
     }
 
@@ -327,7 +327,7 @@ public class LenientAddressParser implements AddressParser {
 
     public AddressList parseAddressList(final CharSequence text) {
         ByteSequence raw = ContentUtil.encode(text);
-        ParserCursor cursor = new ParserCursor(0, text.length());
+        ParserCursor cursor = new ParserCursor(0, raw.length());
         return parseAddressList(raw, cursor);
     }
 
diff --git 
a/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
 
b/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
index b3fdeefc..6f24278b 100644
--- 
a/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
+++ 
b/dom/src/test/java/org/apache/james/mime4j/field/address/LenientAddressBuilderTest.java
@@ -260,6 +260,30 @@ public class LenientAddressBuilderTest {
         Assert.assertEquals("[email protected]", mailbox1.getAddress());
     }
 
+    @Test
+    public void testParseAddressListNonASCII() throws Exception {
+        String wire = "Gr\u00fc\u00dfe <[email protected]>";
+        Mailbox parsed = parser.parseAddressList(wire).flatten().get(0);
+        Assert.assertEquals("Gr\u00fc\u00dfe", parsed.getName());
+        Assert.assertEquals("[email protected]", parsed.getAddress());
+    }
+
+    @Test
+    public void testParseAddressNonASCII() throws Exception {
+        String wire = "Gr\u00fc\u00dfe <[email protected]>";
+        Mailbox parsed = (Mailbox) parser.parseAddress(wire);
+        Assert.assertEquals("Gr\u00fc\u00dfe", parsed.getName());
+        Assert.assertEquals("[email protected]", parsed.getAddress());
+    }
+
+    @Test
+    public void testParseGroupNonASCII() throws Exception {
+        String wire = "Gr\u00fc\u00dfe: [email protected];";
+        Group group = parser.parseGroup(wire);
+        Assert.assertEquals("Gr\u00fc\u00dfe", group.getName());
+        Assert.assertEquals("[email protected]", 
group.getMailboxes().get(0).getAddress());
+    }
+
     @Test
     public void testParsePartialQuotes() throws Exception {
         Mailbox mailbox1 = parser.parseMailbox(


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

Reply via email to