Skip to content

Commit

Permalink
Gossip comm NPE fix
Browse files Browse the repository at this point in the history
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 <yacovm@il.ibm.com>
  • Loading branch information
yacovm committed Dec 5, 2016
1 parent 66ab6fa commit 0af9050
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions gossip/comm/comm_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 0af9050

Please sign in to comment.