From 42d975e5fe91fbe7719422248ad320c04dda5414 Mon Sep 17 00:00:00 2001 From: Zeke Lu Date: Thu, 17 Nov 2022 12:19:49 +0000 Subject: [PATCH] time: avoid creating a parse error from the next chunk of the value When it reports a parse error, it uses the "value" variable as the value element of the parse error. Previously, in some of the cases, the "value" variable is always updated to the next chunk of the value to be parsed (even if an earlier chunk is invalid). The reported parse error is confusing in this case. This CL addresses this issue by holding the original value, and when it fails to parse the time, use it to create the parse error. Fixes #56730. Change-Id: I445b1d8a1b910208d0608b2186881746adb550e0 GitHub-Last-Rev: 67b1102b5e9b345beb2ddcc529a8e608e5afc865 GitHub-Pull-Request: golang/go#56754 Reviewed-on: https://go-review.googlesource.com/c/go/+/450936 Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Joedian Reid Auto-Submit: Bryan Mills Run-TryBot: Bryan Mills --- src/time/format.go | 9 +++++---- src/time/format_test.go | 22 ++++++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/time/format.go b/src/time/format.go index 89a3ce259cf..e4f5750b0c9 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -1067,18 +1067,19 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error) } layout = suffix var p string + hold := value switch std & stdMask { case stdYear: if len(value) < 2 { err = errBad break } - hold := value p, value = value[0:2], value[2:] year, err = atoi(p) if err != nil { - value = hold - } else if year >= 69 { // Unix time starts Dec 31 1969 in some time zones + break + } + if year >= 69 { // Unix time starts Dec 31 1969 in some time zones year += 1900 } else { year += 2000 @@ -1295,7 +1296,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error) return Time{}, newParseError(alayout, avalue, stdstr, value, ": "+rangeErrString+" out of range") } if err != nil { - return Time{}, newParseError(alayout, avalue, stdstr, value, "") + return Time{}, newParseError(alayout, avalue, stdstr, hold, "") } } if pmSet && hour < 12 { diff --git a/src/time/format_test.go b/src/time/format_test.go index b1d85f510be..8a26eaa35ba 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -620,13 +620,13 @@ type ParseErrorTest struct { } var parseErrorTests = []ParseErrorTest{ - {ANSIC, "Feb 4 21:00:60 2010", "cannot parse"}, // cannot parse Feb as Mon - {ANSIC, "Thu Feb 4 21:00:57 @2010", "cannot parse"}, + {ANSIC, "Feb 4 21:00:60 2010", `cannot parse "Feb 4 21:00:60 2010" as "Mon"`}, + {ANSIC, "Thu Feb 4 21:00:57 @2010", `cannot parse "@2010" as "2006"`}, {ANSIC, "Thu Feb 4 21:00:60 2010", "second out of range"}, {ANSIC, "Thu Feb 4 21:61:57 2010", "minute out of range"}, {ANSIC, "Thu Feb 4 24:00:60 2010", "hour out of range"}, - {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", "cannot parse"}, - {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", "cannot parse"}, + {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59x01 2010", `cannot parse "x01 2010" as ".000"`}, + {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.xxx 2010", `cannot parse ".xxx 2010" as ".000"`}, {"Mon Jan _2 15:04:05.000 2006", "Thu Feb 4 23:00:59.-123 2010", "fractional second out of range"}, // issue 4502. StampNano requires exactly 9 digits of precision. {StampNano, "Dec 7 11:22:01.000000", `cannot parse ".000000" as ".000000000"`}, @@ -641,8 +641,8 @@ var parseErrorTests = []ParseErrorTest{ // issue 54569 {RFC3339, "0000-01-01T00:00:.0+00:00", `parsing time "0000-01-01T00:00:.0+00:00" as "2006-01-02T15:04:05Z07:00": cannot parse ".0+00:00" as "05"`}, // issue 21113 - {"_2 Jan 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"}, - {"_2 January 06 15:04 MST", "4 --- 00 00:00 GMT", "cannot parse"}, + {"_2 Jan 06 15:04 MST", "4 --- 00 00:00 GMT", `cannot parse "--- 00 00:00 GMT" as "Jan"`}, + {"_2 January 06 15:04 MST", "4 --- 00 00:00 GMT", `cannot parse "--- 00 00:00 GMT" as "January"`}, // invalid or mismatched day-of-year {"Jan _2 002 2006", "Feb 4 034 2006", "day-of-year does not match day"}, @@ -653,8 +653,14 @@ var parseErrorTests = []ParseErrorTest{ {RFC3339, "\"", `parsing time "\"" as "2006-01-02T15:04:05Z07:00": cannot parse "\"" as "2006"`}, // issue 54570 - {RFC3339, "0000-01-01T00:00:00+00:+0", `parsing time "0000-01-01T00:00:00+00:+0" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"`}, - {RFC3339, "0000-01-01T00:00:00+-0:00", `parsing time "0000-01-01T00:00:00+-0:00" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"`}, + {RFC3339, "0000-01-01T00:00:00+00:+0", `parsing time "0000-01-01T00:00:00+00:+0" as "2006-01-02T15:04:05Z07:00": cannot parse "+00:+0" as "Z07:00"`}, + {RFC3339, "0000-01-01T00:00:00+-0:00", `parsing time "0000-01-01T00:00:00+-0:00" as "2006-01-02T15:04:05Z07:00": cannot parse "+-0:00" as "Z07:00"`}, + + // issue 56730 + {"2006-01-02", "22-10-25", `parsing time "22-10-25" as "2006-01-02": cannot parse "22-10-25" as "2006"`}, + {"06-01-02", "a2-10-25", `parsing time "a2-10-25" as "06-01-02": cannot parse "a2-10-25" as "06"`}, + {"03:04PM", "12:03pM", `parsing time "12:03pM" as "03:04PM": cannot parse "pM" as "PM"`}, + {"03:04pm", "12:03pM", `parsing time "12:03pM" as "03:04pm": cannot parse "pM" as "pm"`}, } func TestParseErrors(t *testing.T) {