Commit 5ef89911 authored by Rob Pike's avatar Rob Pike

time: reject invalid day of month in Parse

There was back-and-forth on this but it has been decided to fix the original
complaint, which was easy.

Fixes #7268.

Change-Id: I6b607c49ad44579086aba2c4f4c5424b97fbed64
Reviewed-on: https://go-review.googlesource.com/17710Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 9ccdc4ed
......@@ -993,6 +993,11 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
hour = 0
}
// Validate the day of the month.
if day > daysIn(Month(month), year) {
return Time{}, &ParseError{alayout, avalue, "", value, ": day of month out of range"}
}
if z != nil {
return Date(year, Month(month), day, hour, min, sec, nsec, z), nil
}
......
......@@ -164,9 +164,6 @@ var parseTests = []ParseTest{
// GMT with offset.
{"GMT-8", UnixDate, "Fri Feb 5 05:00:57 GMT-8 2010", true, true, 1, 0},
// Day of month can be out of range.
{"Jan 36", UnixDate, "Fri Jan 36 05:00:57 GMT-8 2010", true, true, 1, 0},
// Accept any number of fractional second digits (including none) for .999...
// In Go 1, .999... was completely ignored in the format, meaning the first two
// cases would succeed, but the next four would not. Go 1.1 accepts all six.
......@@ -196,6 +193,57 @@ func TestParse(t *testing.T) {
}
}
// All parsed with ANSIC.
var dayOutOfRangeTests = []struct {
date string
ok bool
}{
{"Thu Jan 99 21:00:57 2010", false},
{"Thu Jan 31 21:00:57 2010", true},
{"Thu Jan 32 21:00:57 2010", false},
{"Thu Feb 28 21:00:57 2012", true},
{"Thu Feb 29 21:00:57 2012", true},
{"Thu Feb 29 21:00:57 2010", false},
{"Thu Mar 31 21:00:57 2010", true},
{"Thu Mar 32 21:00:57 2010", false},
{"Thu Apr 30 21:00:57 2010", true},
{"Thu Apr 31 21:00:57 2010", false},
{"Thu May 31 21:00:57 2010", true},
{"Thu May 32 21:00:57 2010", false},
{"Thu Jun 30 21:00:57 2010", true},
{"Thu Jun 31 21:00:57 2010", false},
{"Thu Jul 31 21:00:57 2010", true},
{"Thu Jul 32 21:00:57 2010", false},
{"Thu Aug 31 21:00:57 2010", true},
{"Thu Aug 32 21:00:57 2010", false},
{"Thu Sep 30 21:00:57 2010", true},
{"Thu Sep 31 21:00:57 2010", false},
{"Thu Oct 31 21:00:57 2010", true},
{"Thu Oct 32 21:00:57 2010", false},
{"Thu Nov 30 21:00:57 2010", true},
{"Thu Nov 31 21:00:57 2010", false},
{"Thu Dec 31 21:00:57 2010", true},
{"Thu Dec 32 21:00:57 2010", false},
}
func TestParseDayOutOfRange(t *testing.T) {
for _, test := range dayOutOfRangeTests {
_, err := Parse(ANSIC, test.date)
switch {
case test.ok && err == nil:
// OK
case !test.ok && err != nil:
if !strings.Contains(err.Error(), "day of month out of range") {
t.Errorf("%q: expected 'day of month' error, got %v", test.date, err)
}
case test.ok && err != nil:
t.Errorf("%q: unexpected error: %v", test.date, err)
case !test.ok && err == nil:
t.Errorf("%q: expected 'day of month' error, got none", test.date)
}
}
}
func TestParseInLocation(t *testing.T) {
// Check that Parse (and ParseInLocation) understand that
// Feb 01 AST (Arabia Standard Time) and Feb 01 AST (Atlantic Standard Time)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment