From 36bfcaa96eb16acd0cd1d9f7c8d7395e677dd1b5 Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Thu, 11 Jan 2024 07:01:17 +0530 Subject: [PATCH] bz-68462: Prevent StringIndexOutOfBoundsException when EmailAddress is passed an empty String --- WHATSNEW | 5 +++++ .../apache/tools/ant/taskdefs/email/EmailAddress.java | 3 +++ .../tools/ant/taskdefs/email/EmailAddressTest.java | 11 +++++++++++ 3 files changed, 19 insertions(+) diff --git a/WHATSNEW b/WHATSNEW index 5a9793663d..cc4a0a622c 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -31,6 +31,11 @@ Fixed bugs: command in the JDK supports the "--release" option. Github Pull Request #205 + * Fixes a bug in org.apache.tools.ant.taskdefs.email.EmailAddress which + would throw a java.lang.StringIndexOutOfBoundsException if the email + address passed to its constructor was an empty String. + Bugzilla Report 68462 + Changes from Ant 1.10.13 TO Ant 1.10.14 ======================================= diff --git a/src/main/org/apache/tools/ant/taskdefs/email/EmailAddress.java b/src/main/org/apache/tools/ant/taskdefs/email/EmailAddress.java index 359cbc2081..704174b6e2 100644 --- a/src/main/org/apache/tools/ant/taskdefs/email/EmailAddress.java +++ b/src/main/org/apache/tools/ant/taskdefs/email/EmailAddress.java @@ -117,6 +117,9 @@ public EmailAddress(String email) { * '(', ')', '"', '<', '>' from the start and end of strings */ private String trim(String t, boolean trimAngleBrackets) { + if (t.isEmpty()) { + return t; + } int start = 0; int end = t.length(); boolean trim; diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailAddressTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailAddressTest.java index 9eb17ff486..412e6a8b2b 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailAddressTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailAddressTest.java @@ -93,6 +93,17 @@ public void testD() { expectNameAddress3(new EmailAddress(" < address > ")); } + /** + * verify that an empty value to EmailAddress constructor doesn't lead to + * a StringIndexOutOfBoundsException + */ + @Test + public void testEmpty() { + final EmailAddress addr = new EmailAddress(""); + assertEquals("", addr.getName()); + assertEquals("", addr.getAddress()); + } + private void expectNameAddress(EmailAddress e) { assertEquals("name", e.getName()); assertEquals("address", e.getAddress());