From: Yoshito Umaoka Date: Wed, 16 May 2018 18:59:22 +0000 (+0000) Subject: ICU-13744 Fixed an ICU4J date parsing regression issue causing IndexOutOfBoundsExcept... X-Git-Tag: release-62-rc~100 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cbc23942a709cf2a5a0af40e0b93530855bdae2f;p=icu ICU-13744 Fixed an ICU4J date parsing regression issue causing IndexOutOfBoundsException when pattern has more fields than input date string. X-SVN-Rev: 41383 --- diff --git a/icu4c/source/test/intltest/dtfmttst.cpp b/icu4c/source/test/intltest/dtfmttst.cpp index 0674d7d954d..d6a34235aed 100644 --- a/icu4c/source/test/intltest/dtfmttst.cpp +++ b/icu4c/source/test/intltest/dtfmttst.cpp @@ -5539,6 +5539,15 @@ void DateFormatTest::TestDayPeriodParsing() { k150000, sdf.parse(UnicodeString("2015-11-13 03:00 noon"), errorCode)); } +void DateFormatTest::TestParseRegression13744() { + LocalPointer dfmt(DateFormat::createDateTimeInstance( + DateFormat::SHORT, DateFormat::SHORT, Locale("en", "US"))); + ParsePosition pos(0); + UnicodeString inDate("4/27/18"); + dfmt->parse(inDate, pos); + assertEquals("Error index", inDate.length(), pos.getErrorIndex()); +} + #endif /* #if !UCONFIG_NO_FORMATTING */ //eof diff --git a/icu4c/source/test/intltest/dtfmttst.h b/icu4c/source/test/intltest/dtfmttst.h index 711c6ac8330..efc56732494 100644 --- a/icu4c/source/test/intltest/dtfmttst.h +++ b/icu4c/source/test/intltest/dtfmttst.h @@ -261,6 +261,7 @@ public: void TestDayPeriodWithLocales(); void TestMinuteSecondFieldsInOddPlaces(); void TestDayPeriodParsing(); + void TestParseRegression13744(); private: UBool showParse(DateFormat &format, const UnicodeString &formattedString); 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 8e52d77580e..02e18571db1 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 @@ -2845,7 +2845,7 @@ public class SimpleDateFormat extends DateFormat { char afterType = ((PatternItem) after).type; if (DATE_PATTERN_TYPE.contains(beforeType) != DATE_PATTERN_TYPE.contains(afterType)) { int newPos = originalPos; - while (true) { + while (newPos < tlen) { char ich = text.charAt(newPos); if (!PatternProps.isWhiteSpace(ich)) { break; diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatTest.java index 205fe4e4cf9..b82d1229584 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatTest.java @@ -5422,4 +5422,13 @@ public class DateFormatTest extends TestFmwk { assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 03:00 midnight", k030000, sdf.parse("2015-11-13 03:00 midnight")); assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 03:00 noon", k150000, sdf.parse("2015-11-13 03:00 noon")); } + + @Test + public void TestParseRegression13744() { + DateFormat dfmt = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US); + ParsePosition pos = new ParsePosition(0); + final String inDate = "4/27/18"; // date only, no time + dfmt.parse(inDate, pos); + assertEquals("Error index", inDate.length(), pos.getErrorIndex()); + } }