Skip to content

Commit

Permalink
Fix nil pointer error in FindSuccessors
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Sep 7, 2018
1 parent c143aab commit 8f3bf68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions net/chord/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,17 @@ func InitTCPTransport(listen string, timeout time.Duration) (*TCPTransport, erro

// Checks for a local vnode
func (t *TCPTransport) get(vn *Vnode) (VnodeRPC, bool) {
if vn == nil {
return nil, false
}
key := vn.String()
t.lock.RLock()
defer t.lock.RUnlock()
w, ok := t.local[key]
if ok {
if ok && w != nil && w.obj != nil {
return w.obj, ok
} else {
return nil, ok
return nil, false
}
}

Expand Down Expand Up @@ -768,7 +771,7 @@ func (t *TCPTransport) handleConn(conn *net.TCPConn) {
obj, ok := t.get(body.Target)
resp := tcpBodyVnodeListError{}
sendResp = &resp
if ok {
if ok && obj != nil {
nodes, err := obj.FindSuccessors(body.Num, body.Key)
resp.Vnodes = trimSlice(nodes)
resp.Err = err
Expand Down
7 changes: 5 additions & 2 deletions net/chord/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ func InitLocalTransport(remote Transport) Transport {

// Checks for a local vnode
func (lt *LocalTransport) get(vn *Vnode) (VnodeRPC, bool) {
if vn == nil {
return nil, false
}
key := vn.String()
lt.lock.RLock()
defer lt.lock.RUnlock()
w, ok := lt.local[key]
if ok {
if ok && w != nil && w.obj != nil {
return w.obj, ok
} else {
return nil, ok
return nil, false
}
}

Expand Down
4 changes: 2 additions & 2 deletions net/chord/vnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (vn *localVnode) checkPredecessor() error {
// Finds next N successors. N must be <= NumSuccessors
func (vn *localVnode) FindSuccessors(n int, key []byte) ([]*Vnode, error) {
// Check if we are the immediate predecessor
if betweenRightIncl(vn.Id, vn.successors[0].Id, key) {
if len(vn.successors) > 0 && vn.successors[0] != nil && betweenRightIncl(vn.Id, vn.successors[0].Id, key) {
return vn.successors[:n], nil
}

Expand Down Expand Up @@ -343,7 +343,7 @@ func (vn *localVnode) FindSuccessors(n int, key []byte) ([]*Vnode, error) {

// Check if the ID is between us and any non-immediate successors
for i := 1; i <= successors-n; i++ {
if betweenRightIncl(vn.Id, vn.successors[i].Id, key) {
if vn.successors[i] != nil && betweenRightIncl(vn.Id, vn.successors[i].Id, key) {
remain := vn.successors[i:]
if len(remain) > n {
remain = remain[:n]
Expand Down

0 comments on commit 8f3bf68

Please sign in to comment.