1
0
mirror of https://github.com/golang/go synced 2024-11-21 14:24:44 -07:00

undo CL 6395055 / 2518eee18c4f

Broke TCP selfConnect

««« original CL description
net: avoid nil pointer dereference when RemoteAddr.String method chain is called

Fixes #3721.

R=dave, rsc
CC=golang-dev
https://golang.org/cl/6395055
»»»

R=rsc
CC=golang-dev
https://golang.org/cl/6533043
This commit is contained in:
Mikio Hara 2012-09-19 01:33:03 +09:00
parent e93891f348
commit e4389c008a
5 changed files with 10 additions and 116 deletions

View File

@ -615,7 +615,8 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (netfd *netFD, err e
closesocket(s)
return nil, err
}
netfd.setAddr(localSockname(fd, toAddr), toAddr(rsa))
lsa, _ := syscall.Getsockname(netfd.sysfd)
netfd.setAddr(toAddr(lsa), toAddr(rsa))
return netfd, nil
}

View File

@ -58,13 +58,15 @@ func newFileFD(f *os.File) (*netFD, error) {
}
}
laddr := toAddr(lsa)
rsa, _ := syscall.Getpeername(fd)
raddr := toAddr(rsa)
netfd, err := newFD(fd, family, sotype, laddr.Network())
if err != nil {
closesocket(fd)
return nil, err
}
netfd.setAddr(laddr, remoteSockname(netfd, toAddr))
netfd.setAddr(laddr, raddr)
return netfd, nil
}

View File

@ -14,55 +14,6 @@ import (
"time"
)
var ipConnAddrStringTests = []struct {
net string
laddr string
raddr string
ipv6 bool
}{
{"ip:icmp", "127.0.0.1", "", false},
{"ip:icmp", "::1", "", true},
{"ip:icmp", "", "127.0.0.1", false},
{"ip:icmp", "", "::1", true},
}
func TestIPConnAddrString(t *testing.T) {
if os.Getuid() != 0 {
t.Logf("skipping test; must be root")
return
}
for i, tt := range ipConnAddrStringTests {
if tt.ipv6 && !supportsIPv6 {
continue
}
var (
err error
c *IPConn
mode string
)
if tt.raddr == "" {
mode = "listen"
la, _ := ResolveIPAddr(tt.net, tt.laddr)
c, err = ListenIP(tt.net, la)
if err != nil {
t.Fatalf("ListenIP(%q, %q) failed: %v", tt.net, la.String(), err)
}
} else {
mode = "dial"
la, _ := ResolveIPAddr(tt.net, tt.laddr)
ra, _ := ResolveIPAddr(tt.net, tt.raddr)
c, err = DialIP(tt.net, la, ra)
if err != nil {
t.Fatalf("DialIP(%q, %q) failed: %v", tt.net, ra.String(), err)
}
}
t.Logf("%s-%v: LocalAddr: %q, %q", mode, i, c.LocalAddr(), c.LocalAddr().String())
t.Logf("%s-%v: RemoteAddr: %q, %q", mode, i, c.RemoteAddr(), c.RemoteAddr().String())
c.Close()
}
}
var icmpTests = []struct {
net string
laddr string

View File

@ -58,13 +58,16 @@ func socket(net string, f, t, p int, ipv6only bool, ulsa, ursa syscall.Sockaddr,
fd.isConnected = true
}
lsa, _ := syscall.Getsockname(s)
var laddr Addr
if ulsa != nil && blsa != ulsa {
laddr = toAddr(ulsa)
} else {
laddr = localSockname(fd, toAddr)
laddr = toAddr(lsa)
}
fd.setAddr(laddr, remoteSockname(fd, toAddr))
rsa, _ := syscall.Getpeername(s)
raddr := toAddr(rsa)
fd.setAddr(laddr, raddr)
return fd, nil
}
@ -78,39 +81,3 @@ func genericReadFrom(w io.Writer, r io.Reader) (n int64, err error) {
// Use wrapper to hide existing r.ReadFrom from io.Copy.
return io.Copy(writerOnly{w}, r)
}
func localSockname(fd *netFD, toAddr func(syscall.Sockaddr) Addr) Addr {
sa, _ := syscall.Getsockname(fd.sysfd)
if sa == nil {
return nullProtocolAddr(fd.family, fd.sotype)
}
return toAddr(sa)
}
func remoteSockname(fd *netFD, toAddr func(syscall.Sockaddr) Addr) Addr {
sa, _ := syscall.Getpeername(fd.sysfd)
if sa == nil {
return nullProtocolAddr(fd.family, fd.sotype)
}
return toAddr(sa)
}
func nullProtocolAddr(f, t int) Addr {
switch f {
case syscall.AF_INET, syscall.AF_INET6:
switch t {
case syscall.SOCK_STREAM:
return (*TCPAddr)(nil)
case syscall.SOCK_DGRAM:
return (*UDPAddr)(nil)
case syscall.SOCK_RAW:
return (*IPAddr)(nil)
}
case syscall.AF_UNIX:
switch t {
case syscall.SOCK_STREAM, syscall.SOCK_DGRAM, syscall.SOCK_SEQPACKET:
return (*UnixAddr)(nil)
}
}
panic("unreachable")
}

View File

@ -9,33 +9,6 @@ import (
"testing"
)
var udpConnAddrStringTests = []struct {
net string
laddr string
raddr string
ipv6 bool
}{
{"udp", "127.0.0.1:0", "", false},
{"udp", "[::1]:0", "", true},
}
func TestUDPConnAddrString(t *testing.T) {
for i, tt := range udpConnAddrStringTests {
if tt.ipv6 && !supportsIPv6 {
continue
}
mode := "listen"
la, _ := ResolveUDPAddr(tt.net, tt.laddr)
c, err := ListenUDP(tt.net, la)
if err != nil {
t.Fatalf("ListenUDP(%q, %q) failed: %v", tt.net, la.String(), err)
}
t.Logf("%s-%v: LocalAddr: %q, %q", mode, i, c.LocalAddr(), c.LocalAddr().String())
t.Logf("%s-%v: RemoteAddr: %q, %q", mode, i, c.RemoteAddr(), c.RemoteAddr().String())
c.Close()
}
}
func TestWriteToUDP(t *testing.T) {
switch runtime.GOOS {
case "plan9":