Skip to content

Commit 0af9050

Browse files
committed
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 <[email protected]>
1 parent 66ab6fa commit 0af9050

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

gossip/comm/comm_impl.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ package comm
1818

1919
import (
2020
"bytes"
21+
"crypto/tls"
2122
"fmt"
2223
"math/rand"
2324
"net"
25+
"os"
2426
"sync"
2527
"sync/atomic"
2628
"time"
2729

28-
"crypto/tls"
29-
"os"
30-
3130
"github.com/hyperledger/fabric/gossip/common"
3231
"github.com/hyperledger/fabric/gossip/proto"
3332
"github.com/hyperledger/fabric/gossip/util"
@@ -140,32 +139,36 @@ type commImpl struct {
140139
}
141140

142141
func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidType) (*connection, error) {
142+
var err error
143+
var cc *grpc.ClientConn
144+
var stream proto.Gossip_GossipStreamClient
145+
var pkiID common.PKIidType
146+
143147
c.logger.Debug("Entering", endpoint, expectedPKIID)
144148
defer c.logger.Debug("Exiting")
149+
145150
if c.isStopping() {
146151
return nil, fmt.Errorf("Stopping")
147152
}
148-
cc, err := grpc.Dial(endpoint, append(c.opts, grpc.WithBlock())...)
153+
cc, err = grpc.Dial(endpoint, append(c.opts, grpc.WithBlock())...)
149154
if err != nil {
150-
if cc != nil {
151-
cc.Close()
152-
}
153155
return nil, err
154156
}
155157

156158
cl := proto.NewGossipClient(cc)
157159

158-
if _, err := cl.Ping(context.Background(), &proto.Empty{}); err != nil {
160+
if _, err = cl.Ping(context.Background(), &proto.Empty{}); err != nil {
159161
cc.Close()
160162
return nil, err
161163
}
162164

163-
if stream, err := cl.GossipStream(context.Background()); err == nil {
164-
pkiID, err := c.authenticateRemotePeer(stream)
165+
if stream, err = cl.GossipStream(context.Background()); err == nil {
166+
pkiID, err = c.authenticateRemotePeer(stream)
165167
if err == nil {
166168
if expectedPKIID != nil && !bytes.Equal(pkiID, expectedPKIID) {
167169
// PKIID is nil when we don't know the remote PKI id's
168170
c.logger.Warning("Remote endpoint claims to be a different peer, expected", expectedPKIID, "but got", pkiID)
171+
cc.Close()
169172
return nil, fmt.Errorf("Authentication failure")
170173
}
171174
conn := newConnection(cl, cc, stream, nil)
@@ -183,7 +186,6 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
183186
conn.handler = h
184187
return conn, nil
185188
}
186-
return nil, fmt.Errorf("Authentication failure")
187189
}
188190
cc.Close()
189191
return nil, err

0 commit comments

Comments
 (0)