1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:54:40 -07:00

net: add nil checks to several String methods to avoid panics

Fixes #945.

R=r
CC=golang-dev
https://golang.org/cl/1848049
This commit is contained in:
Andrew Gerrand 2010-07-27 17:22:22 +10:00
parent 8b821696cc
commit fc4ba1546c
5 changed files with 27 additions and 3 deletions

View File

@ -30,6 +30,9 @@ type DNSError struct {
}
func (e *DNSError) String() string {
if e == nil {
return "<nil>"
}
s := "lookup " + e.Name
if e.Server != "" {
s += " on " + e.Server

View File

@ -30,7 +30,12 @@ type IPAddr struct {
// Network returns the address's network name, "ip".
func (a *IPAddr) Network() string { return "ip" }
func (a *IPAddr) String() string { return a.IP.String() }
func (a *IPAddr) String() string {
if a == nil {
return "<nil>"
}
return a.IP.String()
}
func (a *IPAddr) family() int {
if a == nil || len(a.IP) <= 4 {

View File

@ -129,6 +129,9 @@ type OpError struct {
}
func (e *OpError) String() string {
if e == nil {
return "<nil>"
}
s := e.Op
if e.Net != "" {
s += " " + e.Net
@ -164,6 +167,9 @@ type AddrError struct {
}
func (e *AddrError) String() string {
if e == nil {
return "<nil>"
}
s := e.Error
if e.Addr != "" {
s += " " + e.Addr

View File

@ -30,7 +30,12 @@ type TCPAddr struct {
// Network returns the address's network name, "tcp".
func (a *TCPAddr) Network() string { return "tcp" }
func (a *TCPAddr) String() string { return joinHostPort(a.IP.String(), itoa(a.Port)) }
func (a *TCPAddr) String() string {
if a == nil {
return "<nil>"
}
return joinHostPort(a.IP.String(), itoa(a.Port))
}
func (a *TCPAddr) family() int {
if a == nil || len(a.IP) <= 4 {

View File

@ -30,7 +30,12 @@ type UDPAddr struct {
// Network returns the address's network name, "udp".
func (a *UDPAddr) Network() string { return "udp" }
func (a *UDPAddr) String() string { return joinHostPort(a.IP.String(), itoa(a.Port)) }
func (a *UDPAddr) String() string {
if a == nil {
return "<nil>"
}
return joinHostPort(a.IP.String(), itoa(a.Port))
}
func (a *UDPAddr) family() int {
if a == nil || len(a.IP) <= 4 {