]> granicus.if.org Git - icu/commitdiff
ICU-10906 prevent SimpleDateFormat from trying to parse when ParsePosition is less...
authorScott Russell <DTownSMR@gmail.com>
Fri, 13 Jun 2014 14:38:00 +0000 (14:38 +0000)
committerScott Russell <DTownSMR@gmail.com>
Fri, 13 Jun 2014 14:38:00 +0000 (14:38 +0000)
X-SVN-Rev: 35875

icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java

index 4fe544f0cfdb016ed282bbf587b781d8a88bf640..475e3a7183b2fbc5110cecc11aad0007da3e633a 100644 (file)
@@ -2081,6 +2081,10 @@ public class SimpleDateFormat extends DateFormat {
         }
 
         int pos = parsePos.getIndex();
+        if(pos < 0) {
+            parsePos.setErrorIndex(0);
+            return;
+        }
         int start = pos;
 
         Output<TimeType> tzTimeType = new Output<TimeType>(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;
index 704b3ebbc265b46597e6fea0383623c5cadeb0f3..e5f15f34aacfb6525060a8de2b463237daf8ce61 100644 (file)
@@ -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");
+      }
+
+  }
+    
 }