diff options
author | Calin Juravle <calin@google.com> | 2014-04-22 11:49:33 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-04-22 11:49:33 +0000 |
commit | 5d5c657b0101123d8c4de4a316f50a2a76c41a46 (patch) | |
tree | ff003278d088c5d2153aa8daf96eab76110deea6 | |
parent | 0d18ebc5a68d096ce2aa5b5af09bb38453007d7c (diff) | |
parent | 0b2a71a20a34a89fb770acf877c751e0443f46e8 (diff) | |
download | libcore-5d5c657b0101123d8c4de4a316f50a2a76c41a46.zip libcore-5d5c657b0101123d8c4de4a316f50a2a76c41a46.tar.gz libcore-5d5c657b0101123d8c4de4a316f50a2a76c41a46.tar.bz2 |
Merge "Disallow explicit signs in java.sql.Date#valueOf()"
-rw-r--r-- | luni/src/main/java/java/lang/Integer.java | 9 | ||||
-rw-r--r-- | luni/src/main/java/java/sql/Date.java | 26 | ||||
-rw-r--r-- | luni/src/test/java/tests/java/sql/SqlDateTest.java | 50 |
3 files changed, 70 insertions, 15 deletions
diff --git a/luni/src/main/java/java/lang/Integer.java b/luni/src/main/java/java/lang/Integer.java index 111939d..355480a 100644 --- a/luni/src/main/java/java/lang/Integer.java +++ b/luni/src/main/java/java/lang/Integer.java @@ -366,6 +366,15 @@ public final class Integer extends Number implements Comparable<Integer> { } /** + * Equivalent to {@code parsePositiveInt(string, 10)}. + * + * @see #parsePositiveInt(String, int) + */ + public static int parsePositiveInt(String string) throws NumberFormatException { + return parsePositiveInt(string, 10); + } + + /** * Parses the specified string as a positive integer value using the * specified radix. 0 is considered a positive integer. * <p> diff --git a/luni/src/main/java/java/sql/Date.java b/luni/src/main/java/java/sql/Date.java index 4aac326..206d885 100644 --- a/luni/src/main/java/java/sql/Date.java +++ b/luni/src/main/java/java/sql/Date.java @@ -214,23 +214,19 @@ public class Date extends java.util.Date { if (dateString == null) { throw new IllegalArgumentException("dateString == null"); } - int firstIndex = dateString.indexOf('-'); - int secondIndex = dateString.indexOf('-', firstIndex + 1); - // secondIndex == -1 means none or only one separator '-' has been - // found. - // The string is separated into three parts by two separator characters, - // if the first or the third part is null string, we should throw - // IllegalArgumentException to follow RI - if (secondIndex == -1 || firstIndex == 0 - || secondIndex + 1 == dateString.length()) { + if (dateString.length() > 10) { + // early fail to avoid parsing huge invalid strings throw new IllegalArgumentException(); } - // parse each part of the string - int year = Integer.parseInt(dateString.substring(0, firstIndex)); - int month = Integer.parseInt(dateString.substring(firstIndex + 1, - secondIndex)); - int day = Integer.parseInt(dateString.substring(secondIndex + 1, - dateString.length())); + + String[] parts = dateString.split("-"); + if (parts.length != 3) { + throw new IllegalArgumentException(); + } + + int year = Integer.parsePositiveInt(parts[0]); + int month = Integer.parsePositiveInt(parts[1]); + int day = Integer.parsePositiveInt(parts[2]); return new Date(year - 1900, month - 1, day); } diff --git a/luni/src/test/java/tests/java/sql/SqlDateTest.java b/luni/src/test/java/tests/java/sql/SqlDateTest.java new file mode 100644 index 0000000..fb226af --- /dev/null +++ b/luni/src/test/java/tests/java/sql/SqlDateTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.sql.Date; + +import junit.framework.TestCase; + +public class SqlDateTest extends TestCase { + + public void testValueOf() { + String[] dates = { + "2001-12-31", "2001-12-1", "2001-1-1", "1900-12-31" + }; + + for (String date : dates) { + Date.valueOf(date); + } + } + + public void testValueOfInvalidDate() { + String[] invalidDates = { + "", + "+2001-12-31", "2001-+12-31", "2001-12-+31", + "-2001-12-31", "2001--12-31", "2001-12--31", + "2001--","2001--31","-12-31", "-12-", "--31", + "2000000001-12-31" + }; + + for (String date : invalidDates) { + try { + Date.valueOf(date); + fail(); + } catch (IllegalArgumentException expected) { } + } + } + +} |