diff options
author | David Symonds <dsymonds@golang.org> | 2015-08-11 15:05:12 +1000 |
---|---|---|
committer | David Symonds <dsymonds@golang.org> | 2015-08-11 05:07:30 +0000 |
commit | 1d75b40de8ecfaa947e1d61311d3a725fe4ec575 (patch) | |
tree | 7fe7d8a5f2559c9c1593c3969aa0e5655dae366f | |
parent | 546836556d202c9667ad6530710bae6a982d427e (diff) | |
download | go-1d75b40de8ecfaa947e1d61311d3a725fe4ec575.tar.gz go-1d75b40de8ecfaa947e1d61311d3a725fe4ec575.zip |
net/mail: avoid panic in (*Address).String for malformed addresses.
Fixes #12098.
Change-Id: I190586484cd34856dccfafaba60eff0197c7dc20
Reviewed-on: https://go-review.googlesource.com/13500
Reviewed-by: Rob Pike <r@golang.org>
-rw-r--r-- | src/net/mail/message.go | 9 | ||||
-rw-r--r-- | src/net/mail/message_test.go | 8 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/net/mail/message.go b/src/net/mail/message.go index 8ddb313b47..8a89f9b0c0 100644 --- a/src/net/mail/message.go +++ b/src/net/mail/message.go @@ -171,7 +171,14 @@ func (a *Address) String() string { // Format address local@domain at := strings.LastIndex(a.Address, "@") - local, domain := a.Address[:at], a.Address[at+1:] + var local, domain string + if at < 0 { + // This is a malformed address ("@" is required in addr-spec); + // treat the whole address as local-part. + local = a.Address + } else { + local, domain := a.Address[:at], a.Address[at+1:] + } // Add quotes if needed // TODO: rendering quoted local part and rendering printable name diff --git a/src/net/mail/message_test.go b/src/net/mail/message_test.go index ffe9af9bf6..c6b412c181 100644 --- a/src/net/mail/message_test.go +++ b/src/net/mail/message_test.go @@ -483,6 +483,14 @@ func TestAddressFormatting(t *testing.T) { &Address{Name: "Böb Jacöb", Address: "bob@example.com"}, `=?utf-8?q?B=C3=B6b_Jac=C3=B6b?= <bob@example.com>`, }, + { // https://golang.org/issue/12098 + &Address{Name: "Rob", Address: ""}, + `"Rob" <>`, + }, + { // https://golang.org/issue/12098 + &Address{Name: "Rob", Address: "@"}, + `"Rob" <@>`, + }, } for _, test := range tests { s := test.addr.String() |