[DEV-322] Backport btcsuite/btcd PR: connmanager: check Addr for nil (#131)

* [DEV-322] connmanager: check Addr for nil

* [DEV-322] Added crash recovering in TestConnReqString

* [DEV-322] Fixed 'go vet' errors
This commit is contained in:
Evgeny Khirin 2018-12-26 12:38:10 +02:00 committed by stasatdaglabs
parent c45cef0ffe
commit ca084f595e
2 changed files with 20 additions and 1 deletions

View File

@ -89,7 +89,7 @@ func (c *ConnReq) State() ConnState {
// String returns a human-readable string for the connection request.
func (c *ConnReq) String() string {
if c.Addr.String() == "" {
if c.Addr == nil || c.Addr.String() == "" {
return fmt.Sprintf("reqid %d", atomic.LoadUint64(&c.id))
}
return fmt.Sprintf("%s (reqid %d)", c.Addr, atomic.LoadUint64(&c.id))

View File

@ -664,3 +664,22 @@ out:
cmgr.Stop()
cmgr.Wait()
}
// TestConnReqString ensures that ConnReq.String() does not crash
func TestConnReqString(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("ConnReq.String crashed %v", r)
}
}()
cr1 := &ConnReq{
Addr: &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 18555,
},
Permanent: true,
}
_ = cr1.String()
cr2 := &ConnReq{}
_ = cr2.String()
}