mirror of
https://github.com/golang/go
synced 2024-11-22 05:04:40 -07:00
net: make protocol-specific WriteTo, WriteMsg methods return error instead of crash
R=golang-dev, dave, rsc, adg, bradfitz CC=golang-dev https://golang.org/cl/11809043
This commit is contained in:
parent
df7b93c175
commit
7917b88a06
@ -128,6 +128,9 @@ func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
|
|||||||
if !c.ok() {
|
if !c.ok() {
|
||||||
return 0, syscall.EINVAL
|
return 0, syscall.EINVAL
|
||||||
}
|
}
|
||||||
|
if addr == nil {
|
||||||
|
return 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress}
|
||||||
|
}
|
||||||
sa, err := addr.sockaddr(c.fd.family)
|
sa, err := addr.sockaddr(c.fd.family)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, &OpError{"write", c.fd.net, addr, err}
|
return 0, &OpError{"write", c.fd.net, addr, err}
|
||||||
@ -154,6 +157,9 @@ func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error
|
|||||||
if !c.ok() {
|
if !c.ok() {
|
||||||
return 0, 0, syscall.EINVAL
|
return 0, 0, syscall.EINVAL
|
||||||
}
|
}
|
||||||
|
if addr == nil {
|
||||||
|
return 0, 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress}
|
||||||
|
}
|
||||||
sa, err := addr.sockaddr(c.fd.family)
|
sa, err := addr.sockaddr(c.fd.family)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, &OpError{"write", c.fd.net, addr, err}
|
return 0, 0, &OpError{"write", c.fd.net, addr, err}
|
||||||
|
@ -161,6 +161,15 @@ func TestUDPConnSpecificMethods(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if p := recover(); p != nil {
|
||||||
|
t.Fatalf("UDPConn.WriteToUDP or WriteMsgUDP panicked: %v", p)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
c.WriteToUDP(wb, nil)
|
||||||
|
c.WriteMsgUDP(wb, nil, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPConnSpecificMethods(t *testing.T) {
|
func TestIPConnSpecificMethods(t *testing.T) {
|
||||||
@ -218,6 +227,15 @@ func TestIPConnSpecificMethods(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if p := recover(); p != nil {
|
||||||
|
t.Fatalf("IPConn.WriteToIP or WriteMsgIP panicked: %v", p)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
c.WriteToIP(wb, nil)
|
||||||
|
c.WriteMsgIP(wb, nil, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnixListenerSpecificMethods(t *testing.T) {
|
func TestUnixListenerSpecificMethods(t *testing.T) {
|
||||||
@ -358,4 +376,15 @@ func TestUnixConnSpecificMethods(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if p := recover(); p != nil {
|
||||||
|
t.Fatalf("UnixConn.WriteToUnix or WriteMsgUnix panicked: %v", p)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
c1.WriteToUnix(wb, nil)
|
||||||
|
c1.WriteMsgUnix(wb, nil, nil)
|
||||||
|
c3.WriteToUnix(wb, nil)
|
||||||
|
c3.WriteMsgUnix(wb, nil, nil)
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,9 @@ func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
|
|||||||
if !c.ok() || c.fd.data == nil {
|
if !c.ok() || c.fd.data == nil {
|
||||||
return 0, syscall.EINVAL
|
return 0, syscall.EINVAL
|
||||||
}
|
}
|
||||||
|
if addr == nil {
|
||||||
|
return 0, &OpError{Op: "write", Net: c.fd.dir, Addr: nil, Err: errMissingAddress}
|
||||||
|
}
|
||||||
h := new(udpHeader)
|
h := new(udpHeader)
|
||||||
h.raddr = addr.IP.To16()
|
h.raddr = addr.IP.To16()
|
||||||
h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
|
h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
|
||||||
|
@ -124,6 +124,9 @@ func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
|
|||||||
if c.fd.isConnected {
|
if c.fd.isConnected {
|
||||||
return 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
|
return 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
|
||||||
}
|
}
|
||||||
|
if addr == nil {
|
||||||
|
return 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress}
|
||||||
|
}
|
||||||
sa, err := addr.sockaddr(c.fd.family)
|
sa, err := addr.sockaddr(c.fd.family)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, &OpError{"write", c.fd.net, addr, err}
|
return 0, &OpError{"write", c.fd.net, addr, err}
|
||||||
@ -153,6 +156,9 @@ func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err er
|
|||||||
if c.fd.isConnected {
|
if c.fd.isConnected {
|
||||||
return 0, 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
|
return 0, 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
|
||||||
}
|
}
|
||||||
|
if addr == nil {
|
||||||
|
return 0, 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress}
|
||||||
|
}
|
||||||
sa, err := addr.sockaddr(c.fd.family)
|
sa, err := addr.sockaddr(c.fd.family)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, &OpError{"write", c.fd.net, addr, err}
|
return 0, 0, &OpError{"write", c.fd.net, addr, err}
|
||||||
|
@ -186,6 +186,9 @@ func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error) {
|
|||||||
if !c.ok() {
|
if !c.ok() {
|
||||||
return 0, syscall.EINVAL
|
return 0, syscall.EINVAL
|
||||||
}
|
}
|
||||||
|
if addr == nil {
|
||||||
|
return 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress}
|
||||||
|
}
|
||||||
if addr.Net != sotypeToNet(c.fd.sotype) {
|
if addr.Net != sotypeToNet(c.fd.sotype) {
|
||||||
return 0, syscall.EAFNOSUPPORT
|
return 0, syscall.EAFNOSUPPORT
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user