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

peterbourgon/raft: server.go; 69 LoC

Open
#17,637 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 [peterbourgon/raft](https://www.github.com/peterbourgon/raft) at [server.go](https://github.com/peterbourgon/raft/blob/3e45f3d150111fb39d0b962ae51d3816fb170ee5/server.go#L1054-L1122)

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 entry used in defer or goroutine at line 1081

[Click here to see the code in its original context.](https://github.com/peterbourgon/raft/blob/3e45f3d150111fb39d0b962ae51d3816fb170ee5/server.go#L1054-L1122)

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

```go
for i, entry := range r.Entries {
// Configuration changes requre special preprocessing
var pm peerMap
if entry.isConfiguration {
commandBuf := bytes.NewBuffer(entry.Command)
if err := gob.NewDecoder(commandBuf).Decode(&pm); err != nil {
panic("gob decode of peers failed")
}

if s.state.Get() == leader {
// TODO should we instead just ignore this entry?
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf(
"AppendEntry %d/%d failed (configuration): %s",
i+1,
len(r.Entries),
"Leader shouldn't receive configurations via appendEntries",
),
}, stepDown
}

// Expulsion recognition
if _, ok := pm[s.id]; !ok {
entry.committed = make(chan bool)
go func() {
if <-entry.committed {
s.logGeneric("non-leader expelled; shutting down")
q := make(chan struct{})
s.quit <- q
<-q
}
}()
}
}

// Append entry to the log
if err := s.log.appendEntry(entry); err != nil {
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf(
"AppendEntry %d/%d failed: %s",
i+1,
len(r.Entries),
err,
),
}, stepDown
}

// "Once a given server adds the new configuration entry to its log, it
// uses that configuration for all future decisions (it does not wait
// for the entry to become committed)."
if entry.isConfiguration {
if err := s.config.directSet(pm); err != nil {
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf(
"AppendEntry %d/%d failed (configuration): %s",
i+1,
len(r.Entries),
err,
),
}, stepDown
}
}
}

```

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: 3e45f3d150111fb39d0b962ae51d3816fb170ee5

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.