From 4b919b3de0643dbeaf572dcb3afbfd47e57daf44 Mon Sep 17 00:00:00 2001 From: Scott Russell Date: Fri, 13 Jun 2014 14:38:00 +0000 Subject: [PATCH] ICU-10906 prevent SimpleDateFormat from trying to parse when ParsePosition is less than 0 X-SVN-Rev: 35875 --- .../src/com/ibm/icu/text/SimpleDateFormat.java | 8 ++++++-- .../test/format/DateFormatRegressionTest.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java index 4fe544f0cfd..475e3a7183b 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java @@ -2081,6 +2081,10 @@ public class SimpleDateFormat extends DateFormat { } int pos = parsePos.getIndex(); + if(pos < 0) { + parsePos.setErrorIndex(0); + return; + } int start = pos; Output tzTimeType = new Output(TimeType.UNKNOWN); @@ -2114,8 +2118,8 @@ public class SimpleDateFormat extends DateFormat { // try 4/2/2, 3/2/2, 2/2/2, and finally 1/2/2. if (numericFieldStart == -1) { // check if this field is followed by abutting another numeric field - if ((i + 1) < items.length - && (items[i + 1] instanceof PatternItem) + if ((i + 1) < items.length + && (items[i + 1] instanceof PatternItem) && ((PatternItem)items[i + 1]).isNumeric) { // record the first numeric field within a numeric text run numericFieldStart = i; diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java index 704b3ebbc26..e5f15f34aac 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java @@ -1410,4 +1410,22 @@ public class DateFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk { } } + public void TestT10906() + { + String pattern = new String("MM-dd-yyyy"); + String text = new String("06-10-2014"); + SimpleDateFormat format = new SimpleDateFormat(pattern); + ParsePosition pp = new ParsePosition(-1); + try { + format.parse(text, pp); + int errorIdx = pp.getErrorIndex(); + if (errorIdx == -1) { + errln("failed to report invalid (negative) starting parse position"); + } + } catch(StringIndexOutOfBoundsException e) { + errln("failed to fix invalid (negative) starting parse position"); + } + + } + } -- 2.40.0