This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new 1f3b162e Stop ExtendedMessageFormat seekNonWs reading past the pattern
end (#759)
1f3b162e is described below
commit 1f3b162e8300821ec1dd36fb777ba26820f55e57
Author: Javid Khan <[email protected]>
AuthorDate: Fri Jul 3 01:33:43 2026 +0530
Stop ExtendedMessageFormat seekNonWs reading past the pattern end (#759)
Constructor now throws like JRE MessageFormat class it extends:
assertThrowsExactly(IllegalArgumentException.class, () -> new
MessageFormat("{"));
assertThrowsExactly(IllegalArgumentException.class, () -> new
MessageFormat("{0,"));
assertThrowsExactly(IllegalArgumentException.class, () -> new
MessageFormat("{0}extra{"));
---
.../java/org/apache/commons/text/ExtendedMessageFormat.java | 10 ++++++----
.../org/apache/commons/text/ExtendedMessageFormatTest.java | 11 +++++++++++
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
b/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
index d0581390..45adf9f2 100644
--- a/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
+++ b/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
@@ -478,12 +478,14 @@ public class ExtendedMessageFormat extends MessageFormat {
* @param pos current position.
*/
private void seekNonWs(final String pattern, final ParsePosition pos) {
- int len = 0;
final char[] buffer = pattern.toCharArray();
- do {
- len = StringMatcherFactory.INSTANCE.splitMatcher().isMatch(buffer,
pos.getIndex(), 0, buffer.length);
+ while (pos.getIndex() < buffer.length) {
+ final int len =
StringMatcherFactory.INSTANCE.splitMatcher().isMatch(buffer, pos.getIndex(), 0,
buffer.length);
+ if (len == 0) {
+ break;
+ }
pos.setIndex(pos.getIndex() + len);
- } while (len > 0 && pos.getIndex() < pattern.length());
+ }
}
/**
diff --git
a/src/test/java/org/apache/commons/text/ExtendedMessageFormatTest.java
b/src/test/java/org/apache/commons/text/ExtendedMessageFormatTest.java
index 855207ed..a4838f60 100644
--- a/src/test/java/org/apache/commons/text/ExtendedMessageFormatTest.java
+++ b/src/test/java/org/apache/commons/text/ExtendedMessageFormatTest.java
@@ -236,6 +236,17 @@ class ExtendedMessageFormatTest {
assertThrowsExactly(IllegalArgumentException.class, () -> new
ExtendedMessageFormat("{0 ", new HashMap<>()));
}
+ @Test
+ void testTruncatedFormatElementWithRegistry() {
+ // A format element left unterminated at the end of the pattern used
to make the
+ // registry-based constructor seek one past the buffer and throw
+ // ArrayIndexOutOfBoundsException; it should report the documented
+ // IllegalArgumentException, as the plain constructor already does.
+ assertThrowsExactly(IllegalArgumentException.class, () -> new
ExtendedMessageFormat("{", new HashMap<>()));
+ assertThrowsExactly(IllegalArgumentException.class, () -> new
ExtendedMessageFormat("{0,", new HashMap<>()));
+ assertThrowsExactly(IllegalArgumentException.class, () -> new
ExtendedMessageFormat("{0}extra{", new HashMap<>()));
+ }
+
/**
* Test the built in choice format.
*/