From da4a312b80d002d7014e2dfd88df81b210054735 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Sun, 12 Jun 2022 22:08:37 +0200 Subject: [PATCH 1/2] Fail when parsing invalid local date --- .../gson/internal/bind/util/ISO8601Utils.java | 9 +++++-- .../internal/bind/util/ISO8601UtilsTest.java | 27 ++++++++++++++----- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java index 99ec679a71..02454fe3d8 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java +++ b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java @@ -2,7 +2,11 @@ import java.text.ParseException; import java.text.ParsePosition; -import java.util.*; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.TimeZone; /** * 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 { // if the value has no time component (and no time zone), we are done boolean hasT = checkOffset(date, offset, 'T'); - + if (!hasT && (date.length() <= offset)) { Calendar calendar = new GregorianCalendar(year, month - 1, day); + calendar.setLenient(false); pos.setIndex(offset); return calendar.getTime(); diff --git a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java index bc0c9ec0f6..68c6e8dd73 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java @@ -1,13 +1,18 @@ package com.google.gson.internal.bind.util; -import org.junit.Test; -import org.junit.function.ThrowingRunnable; -import java.text.ParseException; -import java.text.ParsePosition; -import java.util.*; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; +import static org.junit.Assert.fail; + +import java.text.ParseException; +import java.text.ParsePosition; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.TimeZone; +import org.junit.Test; +import org.junit.function.ThrowingRunnable; public class ISO8601UtilsTest { @@ -61,6 +66,16 @@ public void testDateParseWithDefaultTimezone() throws ParseException { assertEquals(expectedDate, date); } + @Test + public void testDateParseInvalid() { + String dateStr = "2022-15-01"; + try { + ISO8601Utils.parse(dateStr, new ParsePosition(0)); + fail(); + } catch (ParseException e) { + } + } + @Test public void testDateParseWithTimezone() throws ParseException { String dateStr = "2018-06-25T00:00:00-03:00"; From 25e92b60737daa7db19e7caf9771c656c24b0a90 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Mon, 13 Jun 2022 00:03:14 +0200 Subject: [PATCH 2/2] Improve invalid date tests --- .../internal/bind/util/ISO8601UtilsTest.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java index 68c6e8dd73..68db238aaf 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java @@ -67,12 +67,22 @@ public void testDateParseWithDefaultTimezone() throws ParseException { } @Test - public void testDateParseInvalid() { - String dateStr = "2022-15-01"; + public void testDateParseInvalidDay() { + String dateStr = "2022-12-33"; try { ISO8601Utils.parse(dateStr, new ParsePosition(0)); - fail(); - } catch (ParseException e) { + fail("Expected parsing to fail"); + } catch (ParseException expected) { + } + } + + @Test + public void testDateParseInvalidMonth() { + String dateStr = "2022-14-30"; + try { + ISO8601Utils.parse(dateStr, new ParsePosition(0)); + fail("Expected parsing to fail"); + } catch (ParseException expected) { } }