From 0af905034c2f8061bd5094fc8a821a7d88d47e48 Mon Sep 17 00:00:00 2001 From: YACOVM Date: Mon, 5 Dec 2016 09:49:44 +0200 Subject: [PATCH] Gossip comm NPE fix Fix a possible NPE situation. Error returned before the fix could be nil, therefore nil, nil was returned instead of nil and an error Change-Id: I6c4b592be63626cc7a11f81b19311fe71361c95b Signed-off-by: Yacov Manevich --- gossip/comm/comm_impl.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/gossip/comm/comm_impl.go b/gossip/comm/comm_impl.go index c2605f2e809..32465e05448 100644 --- a/gossip/comm/comm_impl.go +++ b/gossip/comm/comm_impl.go @@ -18,16 +18,15 @@ package comm import ( "bytes" + "crypto/tls" "fmt" "math/rand" "net" + "os" "sync" "sync/atomic" "time" - "crypto/tls" - "os" - "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/proto" "github.com/hyperledger/fabric/gossip/util" @@ -140,32 +139,36 @@ type commImpl struct { } func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidType) (*connection, error) { + var err error + var cc *grpc.ClientConn + var stream proto.Gossip_GossipStreamClient + var pkiID common.PKIidType + c.logger.Debug("Entering", endpoint, expectedPKIID) defer c.logger.Debug("Exiting") + if c.isStopping() { return nil, fmt.Errorf("Stopping") } - cc, err := grpc.Dial(endpoint, append(c.opts, grpc.WithBlock())...) + cc, err = grpc.Dial(endpoint, append(c.opts, grpc.WithBlock())...) if err != nil { - if cc != nil { - cc.Close() - } return nil, err } cl := proto.NewGossipClient(cc) - if _, err := cl.Ping(context.Background(), &proto.Empty{}); err != nil { + if _, err = cl.Ping(context.Background(), &proto.Empty{}); err != nil { cc.Close() return nil, err } - if stream, err := cl.GossipStream(context.Background()); err == nil { - pkiID, err := c.authenticateRemotePeer(stream) + if stream, err = cl.GossipStream(context.Background()); err == nil { + pkiID, err = c.authenticateRemotePeer(stream) if err == nil { if expectedPKIID != nil && !bytes.Equal(pkiID, expectedPKIID) { // PKIID is nil when we don't know the remote PKI id's c.logger.Warning("Remote endpoint claims to be a different peer, expected", expectedPKIID, "but got", pkiID) + cc.Close() return nil, fmt.Errorf("Authentication failure") } conn := newConnection(cl, cc, stream, nil) @@ -183,7 +186,6 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT conn.handler = h return conn, nil } - return nil, fmt.Errorf("Authentication failure") } cc.Close() return nil, err