github-vet / github-vet/rangeloop-pointer-findings

salamer/naive_raft: node.go; 52 LoC

Open
#13,845 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [salamer/naive_raft](https://www.github.com/salamer/naive_raft) at [node.go](https://github.com/salamer/naive_raft/blob/c1e72c2627f5bcccb712dff095c6487e9c8fbf15/node.go#L240-L291)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable sibling used in defer or goroutine at line 250

[Click here to see the code in its original context.](https://github.com/salamer/naive_raft/blob/c1e72c2627f5bcccb712dff095c6487e9c8fbf15/node.go#L240-L291)

Click here to show the 52 line(s) of Go which triggered the analyzer.

```go
for _, sibling := range node.siblingNodes {
if sibling.ID != node.id {
go func(host string, port int) {
conn, e := grpc.Dial(host+":"+strconv.Itoa(port), grpc.WithInsecure())
if e != nil {
fmt.Printf("did not connect: %v", e)
}

c := pb.NewAppendEntriesClient(conn)
node.observersLock.RLock()
ress := node.getEntris(node.nextIndex[sibling.ID], len(node.log))
fmt.Printf("res:%+v\n", ress)
var _ress []*pb.LogEntris
for i := 0; i < len(ress); i++ {

// fix golang pointer convert 0 error
idx := int32(ress[i].idx)
if idx == 0 {
idx = int32(ZERO_REPLACE_NUM)
}

_ress = append(_ress, &pb.LogEntris{
Index: idx, //can't not convert 0 in golang
Term: int32(ress[i].term),
Data: ress[i].data,
})
}

result, err := c.AppendEntriesRPC(context.Background(), &pb.AppendEntriesReq{
Term: int32(node.currentTerm),
LeaderId: int32(node.id),
PrevLogIndex: int32(node.nextIndex[sibling.ID] - 2), //the previous log index
PrevTermIndex: int32(node.currentTerm),
LogEntris: _ress,
LeaderCommit: int32(node.commitIndex),
})
if err != nil {
fmt.Printf("append entries error: %v\n", err)
}
fmt.Printf("result:%+v,%+v\n", _ress, ress)
if result.Success {
node.nextIndex[sibling.ID] += (len(node.log) - node.nextIndex[sibling.ID])
} else {
if node.nextIndex[sibling.ID] > 0 {
node.nextIndex[sibling.ID] -= 1
}
}
node.observersLock.RUnlock()
defer conn.Close()
}(sibling.Host, sibling.Port)
}
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: c1e72c2627f5bcccb712dff095c6487e9c8fbf15

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.