aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2010-07-27 17:22:22 +1000
committerAndrew Gerrand <adg@golang.org>2010-07-27 17:22:22 +1000
commitfc4ba1546c787a99fbcd037649575c29728655b4 (patch)
treeefc66406582543a48abf37ae55cfe0926a6135d5
parent8b821696cc96ea3167c16138beff9ca1ecc5f1ed (diff)
downloadgo-fc4ba1546c787a99fbcd037649575c29728655b4.tar.gz
go-fc4ba1546c787a99fbcd037649575c29728655b4.zip
net: add nil checks to several String methods to avoid panics
Fixes #945. R=r CC=golang-dev https://golang.org/cl/1848049
-rw-r--r--src/pkg/net/dnsclient.go3
-rw-r--r--src/pkg/net/iprawsock.go7
-rw-r--r--src/pkg/net/net.go6
-rw-r--r--src/pkg/net/tcpsock.go7
-rw-r--r--src/pkg/net/udpsock.go7
5 files changed, 27 insertions, 3 deletions
diff --git a/src/pkg/net/dnsclient.go b/src/pkg/net/dnsclient.go
index ea21117e3c..fe54f6b12a 100644
--- a/src/pkg/net/dnsclient.go
+++ b/src/pkg/net/dnsclient.go
@@ -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
diff --git a/src/pkg/net/iprawsock.go b/src/pkg/net/iprawsock.go
index bd8f8080ad..6b48512e00 100644
--- a/src/pkg/net/iprawsock.go
+++ b/src/pkg/net/iprawsock.go
@@ -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 {
diff --git a/src/pkg/net/net.go b/src/pkg/net/net.go
index 0474478700..c0c1c3b8ab 100644
--- a/src/pkg/net/net.go
+++ b/src/pkg/net/net.go
@@ -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
diff --git a/src/pkg/net/tcpsock.go b/src/pkg/net/tcpsock.go
index 7a60cd2e7d..eb846694ba 100644
--- a/src/pkg/net/tcpsock.go
+++ b/src/pkg/net/tcpsock.go
@@ -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 {
diff --git a/src/pkg/net/udpsock.go b/src/pkg/net/udpsock.go
index 6ea0f27531..89a074755b 100644
--- a/src/pkg/net/udpsock.go
+++ b/src/pkg/net/udpsock.go
@@ -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 {