From b252fe70026a0e7b4bbf3ec6f4f74cf02e3c73b7 Mon Sep 17 00:00:00 2001 From: Mikio Hara Date: Wed, 25 Apr 2012 12:29:14 +0900 Subject: [PATCH] net: fix crash of Listen with "" or nil laddr Fixes #3584. R=dave, dsymonds, rsc CC=golang-dev https://golang.org/cl/6119043 --- src/pkg/net/ipsock_posix.go | 5 ++++- src/pkg/net/unicast_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/pkg/net/ipsock_posix.go b/src/pkg/net/ipsock_posix.go index ed313195c9..171889207d 100644 --- a/src/pkg/net/ipsock_posix.go +++ b/src/pkg/net/ipsock_posix.go @@ -97,10 +97,13 @@ func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family return syscall.AF_INET6, true } - if mode == "listen" && laddr.isWildcard() { + if mode == "listen" && (laddr == nil || laddr.isWildcard()) { if supportsIPv4map { return syscall.AF_INET6, false } + if laddr == nil { + return syscall.AF_INET, false + } return laddr.family(), false } diff --git a/src/pkg/net/unicast_test.go b/src/pkg/net/unicast_test.go index e5dd013db6..7b3b19b323 100644 --- a/src/pkg/net/unicast_test.go +++ b/src/pkg/net/unicast_test.go @@ -536,3 +536,33 @@ func TestProhibitionaryDialArgs(t *testing.T) { } } } + +func TestWildWildcardListener(t *testing.T) { + switch runtime.GOOS { + case "plan9": + t.Logf("skipping test on %q", runtime.GOOS) + return + } + + defer func() { + if recover() != nil { + t.Fatalf("panicked") + } + }() + + if ln, err := Listen("tcp", ""); err != nil { + ln.Close() + } + if ln, err := ListenPacket("udp", ""); err != nil { + ln.Close() + } + if ln, err := ListenTCP("tcp", nil); err != nil { + ln.Close() + } + if ln, err := ListenUDP("udp", nil); err != nil { + ln.Close() + } + if ln, err := ListenIP("ip:icmp", nil); err != nil { + ln.Close() + } +}