Skip to content

Commit 57225c6

Browse files
authored
Fail when parsing invalid local date (#2134)
* Fail when parsing invalid local date * Improve invalid date tests
1 parent 96ab171 commit 57225c6

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.text.ParseException;
44
import java.text.ParsePosition;
5-
import java.util.*;
5+
import java.util.Calendar;
6+
import java.util.Date;
7+
import java.util.GregorianCalendar;
8+
import java.util.Locale;
9+
import java.util.TimeZone;
610

711
/**
812
* Utilities methods for manipulating dates in iso8601 format. This is much much faster and GC friendly than using SimpleDateFormat so
@@ -147,9 +151,10 @@ public static Date parse(String date, ParsePosition pos) throws ParseException {
147151

148152
// if the value has no time component (and no time zone), we are done
149153
boolean hasT = checkOffset(date, offset, 'T');
150-
154+
151155
if (!hasT && (date.length() <= offset)) {
152156
Calendar calendar = new GregorianCalendar(year, month - 1, day);
157+
calendar.setLenient(false);
153158

154159
pos.setIndex(offset);
155160
return calendar.getTime();

gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.google.gson.internal.bind.util;
22

3-
import org.junit.Test;
4-
import org.junit.function.ThrowingRunnable;
5-
import java.text.ParseException;
6-
import java.text.ParsePosition;
7-
import java.util.*;
8-
93
import static org.junit.Assert.assertEquals;
104
import static org.junit.Assert.assertThrows;
5+
import static org.junit.Assert.fail;
6+
7+
import java.text.ParseException;
8+
import java.text.ParsePosition;
9+
import java.util.Calendar;
10+
import java.util.Date;
11+
import java.util.GregorianCalendar;
12+
import java.util.Locale;
13+
import java.util.TimeZone;
14+
import org.junit.Test;
15+
import org.junit.function.ThrowingRunnable;
1116

1217
public class ISO8601UtilsTest {
1318

@@ -61,6 +66,26 @@ public void testDateParseWithDefaultTimezone() throws ParseException {
6166
assertEquals(expectedDate, date);
6267
}
6368

69+
@Test
70+
public void testDateParseInvalidDay() {
71+
String dateStr = "2022-12-33";
72+
try {
73+
ISO8601Utils.parse(dateStr, new ParsePosition(0));
74+
fail("Expected parsing to fail");
75+
} catch (ParseException expected) {
76+
}
77+
}
78+
79+
@Test
80+
public void testDateParseInvalidMonth() {
81+
String dateStr = "2022-14-30";
82+
try {
83+
ISO8601Utils.parse(dateStr, new ParsePosition(0));
84+
fail("Expected parsing to fail");
85+
} catch (ParseException expected) {
86+
}
87+
}
88+
6489
@Test
6590
public void testDateParseWithTimezone() throws ParseException {
6691
String dateStr = "2018-06-25T00:00:00-03:00";

0 commit comments

Comments
 (0)