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 d4b4d8c35 Keep FastDateParser numeric parse from throwing on int 
overflow (#1741)
d4b4d8c35 is described below

commit d4b4d8c352d048f5dab3c85dd6ef380cfe04bdee
Author: alhuda <[email protected]>
AuthorDate: Sat Jul 4 23:53:00 2026 +0530

    Keep FastDateParser numeric parse from throwing on int overflow (#1741)
---
 .../org/apache/commons/lang3/time/FastDateParser.java     | 10 +++++++++-
 .../org/apache/commons/lang3/time/FastDateParserTest.java | 15 +++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

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 21454f375..8966fc3f0 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
@@ -305,7 +305,15 @@ boolean parse(final FastDateParser parser, final Calendar 
calendar, final String
                 pos.setErrorIndex(idx);
                 return false;
             }
-            final int value = 
Integer.parseInt(source.substring(pos.getIndex(), idx));
+            final int value;
+            try {
+                value = Integer.parseInt(source.substring(pos.getIndex(), 
idx));
+            } catch (final NumberFormatException nfe) {
+                // A run of digits that overflows int cannot be represented by 
this field; signal a parse failure
+                // rather than letting NumberFormatException escape the 
ParsePosition-based parse methods.
+                pos.setErrorIndex(pos.getIndex());
+                return false;
+            }
             pos.setIndex(idx);
             calendar.set(field, modify(parser, value));
             return true;
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 4e07086a6..7847cf62b 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
@@ -21,6 +21,7 @@
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
@@ -411,6 +412,20 @@ void testLang1121(final TriFunction<String, TimeZone, 
Locale, DateParser> dpProv
         assertEquals(expected, actual);
     }
 
+    @ParameterizedTest
+    @MethodSource(DATE_PARSER_PARAMETERS)
+    void testLang1359(final TriFunction<String, TimeZone, Locale, DateParser> 
dpProvider) {
+        // A trailing numeric field is unbounded, so a digit run that 
overflows int must fail the parse
+        // through the ParsePosition/ParseException contract, not escape as a 
NumberFormatException.
+        final DateParser fdp = getInstance(dpProvider, "yyyy", TimeZones.GMT, 
Locale.US);
+        final String overflow = "99999999999";
+        assertThrows(ParseException.class, () -> fdp.parse(overflow));
+        final ParsePosition pos = new ParsePosition(0);
+        assertNull(fdp.parseObject(overflow, pos));
+        assertTrue(pos.getErrorIndex() >= 0);
+        assertEquals(0, pos.getIndex());
+    }
+
     @ParameterizedTest
     @MethodSource(DATE_PARSER_PARAMETERS)
     void testLang1380(final TriFunction<String, TimeZone, Locale, DateParser> 
dpProvider) throws ParseException {

Reply via email to