Commit 1d75b40d authored by David Symonds's avatar David Symonds

net/mail: avoid panic in (*Address).String for malformed addresses.

Fixes #12098.

Change-Id: I190586484cd34856dccfafaba60eff0197c7dc20
Reviewed-on: https://go-review.googlesource.com/13500Reviewed-by: default avatarRob Pike <r@golang.org>
parent 54683655
......@@ -171,7 +171,14 @@ func (a *Address) String() string {
// Format address local@domain
at := strings.LastIndex(a.Address, "@")
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
......
......@@ -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()
......
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