From 1b09d430678d4a6f73b2443463d11f75851aba8a Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 16 Oct 2020 00:49:02 -0400 Subject: all: update references to symbols moved from io/ioutil to io The old ioutil references are still valid, but update our code to reflect best practices and get used to the new locations. Code compiled with the bootstrap toolchain (cmd/asm, cmd/dist, cmd/compile, debug/elf) must remain Go 1.4-compatible and is excluded. Also excluded vendored code. For #41190. Change-Id: I6d86f2bf7bc37a9d904b6cee3fe0c7af6d94d5b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/263142 Trust: Russ Cox Run-TryBot: Russ Cox TryBot-Result: Go Bot Reviewed-by: Emmanuel Odeke --- src/net/mail/example_test.go | 4 ++-- src/net/mail/message_test.go | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'src/net/mail') diff --git a/src/net/mail/example_test.go b/src/net/mail/example_test.go index c3365642aa..d325dc791f 100644 --- a/src/net/mail/example_test.go +++ b/src/net/mail/example_test.go @@ -6,7 +6,7 @@ package mail_test import ( "fmt" - "io/ioutil" + "io" "log" "net/mail" "strings" @@ -62,7 +62,7 @@ Message body fmt.Println("To:", header.Get("To")) fmt.Println("Subject:", header.Get("Subject")) - body, err := ioutil.ReadAll(m.Body) + body, err := io.ReadAll(m.Body) if err != nil { log.Fatal(err) } diff --git a/src/net/mail/message_test.go b/src/net/mail/message_test.go index 67e3643aeb..188d0bf766 100644 --- a/src/net/mail/message_test.go +++ b/src/net/mail/message_test.go @@ -7,7 +7,6 @@ package mail import ( "bytes" "io" - "io/ioutil" "mime" "reflect" "strings" @@ -53,7 +52,7 @@ func TestParsing(t *testing.T) { t.Errorf("test #%d: Incorrectly parsed message header.\nGot:\n%+v\nWant:\n%+v", i, msg.Header, test.header) } - body, err := ioutil.ReadAll(msg.Body) + body, err := io.ReadAll(msg.Body) if err != nil { t.Errorf("test #%d: Failed reading body: %v", i, err) continue @@ -842,7 +841,7 @@ func TestAddressParser(t *testing.T) { ap := AddressParser{WordDecoder: &mime.WordDecoder{ CharsetReader: func(charset string, input io.Reader) (io.Reader, error) { - in, err := ioutil.ReadAll(input) + in, err := io.ReadAll(input) if err != nil { return nil, err } -- cgit v1.3 From 3ef8562c9c2c7f6897572b05b70ac936a99fd043 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Tue, 26 May 2020 14:14:08 -0700 Subject: net/mail: avoid ParseDate confusion if day name includes "T" Fixes the check for RFC 5322 "obsolete time zone" to ensure that we correctly extract the entire date from the "T" of the implied time zone. Obsolete Time zones come in the form: * GMT * PST * MDT etc, as per Section 4.3 of RFC 5322, https://tools.ietf.org/html/rfc5322#section-4.3. The prior check from CL 117596 erronenously used strings.Index which selects the first "T", and that meant that dates containing days "Tue" or "Thu" could not be parsed. We also now deal with "T" in the CFWS "Comment Folding White Space". Thus we'll now accept dates: * Thu, 20 Nov 1997 09:55:06 MDT * Thu, 20 Nov 1997 09:55:06 MDT (MDT) * Fri, 21 Nov 1997 09:55:06 MDT (This comment) * Fri, 21 Nov 1997 09:55:06 MDT (MDT Fixes #39260 Change-Id: I6d59d99bc4f05a82582c826b5c5a080a25fd999b Reviewed-on: https://go-review.googlesource.com/c/go/+/235200 Run-TryBot: Emmanuel Odeke TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor Trust: Emmanuel Odeke --- src/net/mail/message.go | 24 +++++++++++++++++++----- src/net/mail/message_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) (limited to 'src/net/mail') diff --git a/src/net/mail/message.go b/src/net/mail/message.go index 09fb794005..47bbf6ca97 100644 --- a/src/net/mail/message.go +++ b/src/net/mail/message.go @@ -112,11 +112,25 @@ func ParseDate(date string) (time.Time, error) { if ind := strings.IndexAny(p.s, "+-"); ind != -1 && len(p.s) >= ind+5 { date = p.s[:ind+5] p.s = p.s[ind+5:] - } else if ind := strings.Index(p.s, "T"); ind != -1 && len(p.s) >= ind+1 { - // The last letter T of the obsolete time zone is checked when no standard time zone is found. - // If T is misplaced, the date to parse is garbage. - date = p.s[:ind+1] - p.s = p.s[ind+1:] + } else { + ind := strings.Index(p.s, "T") + if ind == 0 { + // In this case we have the following date formats: + // * Thu, 20 Nov 1997 09:55:06 MDT + // * Thu, 20 Nov 1997 09:55:06 MDT (MDT) + // * Thu, 20 Nov 1997 09:55:06 MDT (This comment) + ind = strings.Index(p.s[1:], "T") + if ind != -1 { + ind++ + } + } + + if ind != -1 && len(p.s) >= ind+5 { + // The last letter T of the obsolete time zone is checked when no standard time zone is found. + // If T is misplaced, the date to parse is garbage. + date = p.s[:ind+1] + p.s = p.s[ind+1:] + } } if !p.skipCFWS() { return time.Time{}, errors.New("mail: misformatted parenthetical comment") diff --git a/src/net/mail/message_test.go b/src/net/mail/message_test.go index 188d0bf766..0daa3d6c63 100644 --- a/src/net/mail/message_test.go +++ b/src/net/mail/message_test.go @@ -102,6 +102,18 @@ func TestDateParsing(t *testing.T) { "Fri, 21 Nov 1997 09:55:06 -0600 (MDT)", time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), }, + { + "Thu, 20 Nov 1997 09:55:06 -0600 (MDT)", + time.Date(1997, 11, 20, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), + }, + { + "Thu, 20 Nov 1997 09:55:06 MDT (MDT)", + time.Date(1997, 11, 20, 9, 55, 6, 0, time.FixedZone("MDT", 0)), + }, + { + "Fri, 21 Nov 1997 09:55:06 +1300 (TOT)", + time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", +13*60*60)), + }, } for _, test := range tests { hdr := Header{ @@ -243,6 +255,33 @@ func TestDateParsingCFWS(t *testing.T) { time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), false, }, + // Ensure that the presence of "T" in the date + // doesn't trip out ParseDate, as per issue 39260. + { + "Tue, 26 May 2020 14:04:40 GMT", + time.Date(2020, 05, 26, 14, 04, 40, 0, time.UTC), + true, + }, + { + "Tue, 26 May 2020 14:04:40 UT", + time.Date(2020, 05, 26, 14, 04, 40, 0, time.UTC), + false, + }, + { + "Thu, 21 May 2020 14:04:40 UT", + time.Date(2020, 05, 21, 14, 04, 40, 0, time.UTC), + false, + }, + { + "Thu, 21 May 2020 14:04:40 UTC", + time.Date(2020, 05, 21, 14, 04, 40, 0, time.UTC), + true, + }, + { + "Fri, 21 Nov 1997 09:55:06 MDT (MDT)", + time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("MDT", 0)), + true, + }, } for _, test := range tests { hdr := Header{ -- cgit v1.3