net: do not panic on nil IPNet.String()

The code looks like it was already trying to avoid this but missed a
spot.

Fixes #50271.

Change-Id: I450adac3f618b9535b61a28e6a160eacc351d47c
Reviewed-on: https://go-review.googlesource.com/c/go/+/373075
Trust: Jason Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Jason A. Donenfeld 2022-01-05 00:16:40 +01:00 committed by Jason Donenfeld
parent f154f8b5bb
commit 301db3f5d2
2 changed files with 4 additions and 0 deletions

View File

@ -545,6 +545,9 @@ func (n *IPNet) Network() string { return "ip+net" }
// character and a mask expressed as hexadecimal form with no
// punctuation like "198.51.100.0/c000ff00".
func (n *IPNet) String() string {
if n == nil {
return "<nil>"
}
nn, m := networkNumberAndMask(n)
if nn == nil || m == nil {
return "<nil>"

View File

@ -407,6 +407,7 @@ var ipNetStringTests = []struct {
{&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192.168.1.0/ff00ff00"},
{&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8::/55"},
{&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:cafe::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
{nil, "<nil>"},
}
func TestIPNetString(t *testing.T) {