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-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new e0dba35f9 [LANG-1833] Handle ParsePosition index beyond source string 
length cleanly (#1757)
e0dba35f9 is described below

commit e0dba35f9e41dfdbbbb646fcd377b8af8ef7344e
Author: Maksym Korshun <[email protected]>
AuthorDate: Tue Jul 28 00:08:58 2026 +0200

    [LANG-1833] Handle ParsePosition index beyond source string length cleanly 
(#1757)
---
 .../apache/commons/lang3/time/FastDateParser.java   | 10 ++++++++++
 .../commons/lang3/time/FastDateParserTest.java      | 21 +++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java 
b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
index 01c3df890..0a4177790 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
@@ -1043,6 +1043,11 @@ public Date parse(final String source) throws 
ParseException {
      */
     @Override
     public Date parse(final String source, final ParsePosition pos) {
+        final int startIndex = pos.getIndex();
+        if (startIndex > source.length()) {
+            pos.setErrorIndex(startIndex);
+            return null;
+        }
         // timing tests indicate getting new instance is 19% faster than 
cloning
         final Calendar cal = Calendar.getInstance(timeZone, locale);
         cal.clear();
@@ -1062,6 +1067,11 @@ public Date parse(final String source, final 
ParsePosition pos) {
      */
     @Override
     public boolean parse(final String source, final ParsePosition pos, final 
Calendar calendar) {
+        final int startIndex = pos.getIndex();
+        if (startIndex > source.length()) {
+            pos.setErrorIndex(startIndex);
+            return false;
+        }
         final ListIterator<StrategyAndWidth> lt = patterns.listIterator();
         while (lt.hasNext()) {
             final StrategyAndWidth strategyAndWidth = lt.next();
diff --git 
a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java 
b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
index 6d12c97ab..2cf6ebf8a 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
@@ -563,6 +563,27 @@ void testParseNumerics(final TriFunction<String, TimeZone, 
Locale, DateParser> d
         assertEquals(cal.getTime(), fdf.parse("20030210153320989"));
     }
 
+    @Test
+    public void testParsePositionBeyondInputLength() {
+        final String source = "Jan";
+        final int startingIndex = 10;
+        final String[] patterns = new String[] {"yyyy", "MM", "dd", "HH", 
"'x'", "-", "/", ":", " 'at' ", "MMM", "EEEE", "a", "z"};
+        for (final String pattern : patterns) {
+            final DateParser parser = getInstance(pattern);
+            final ParsePosition pos1 = new ParsePosition(startingIndex);
+            final Date date = parser.parse(source, pos1);
+            assertNull(date);
+            assertEquals(startingIndex, pos1.getIndex());
+            assertEquals(startingIndex, pos1.getErrorIndex());
+            final ParsePosition pos2 = new ParsePosition(startingIndex);
+            final Calendar cal = Calendar.getInstance();
+            final boolean success = parser.parse(source, pos2, cal);
+            assertFalse(success);
+            assertEquals(startingIndex, pos2.getIndex());
+            assertEquals(startingIndex, pos2.getErrorIndex());
+        }
+    }
+
     @Test
     void testParseOffset() {
         final DateParser parser = getInstance(YMD_SLASH);

Reply via email to