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

brynbellomy/redwood: host.go; 69 LoC

Open
#6,732 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 [brynbellomy/redwood](https://www.github.com/brynbellomy/redwood) at [host.go](https://github.com/brynbellomy/redwood/blob/6b3e915822f3dfa8e31c721041d1781c0bdbc7e3/host.go#L936-L1004)

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 peer used in defer or goroutine at line 969

[Click here to see the code in its original context.](https://github.com/brynbellomy/redwood/blob/6b3e915822f3dfa8e31c721041d1781c0bdbc7e3/host.go#L936-L1004)

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

```go
for peer := range h.ProvidersOfRef(ctx, refID) {
err := peer.EnsureConnected(ctx)
if err != nil {
h.Errorf("error connecting to peer: %v", err)
continue
}

err = peer.FetchRef(refID)
if err != nil {
h.Errorf("error writing to peer: %v", err)
continue
}

// Not currently used
_, err = peer.ReceiveRefHeader()
if err != nil {
h.Errorf("error reading from peer: %v", err)
continue
}

pr, pw := io.Pipe()
go func() {
var err error
defer func() { pw.CloseWithError(err) }()

for {
select {
case <-ctx.Done():
err = ctx.Err()
return
default:
}

pkt, err := peer.ReceiveRefPacket()
if err != nil {
h.Errorf("error receiving ref from peer: %v", err)
return
} else if pkt.End {
return
}

var n int
n, err = pw.Write(pkt.Data)
if err != nil {
h.Errorf("error receiving ref from peer: %v", err)
return
} else if n < len(pkt.Data) {
err = io.ErrUnexpectedEOF
return
}
}
}()

sha1Hash, sha3Hash, err := h.refStore.StoreObject(pr)
if err != nil {
h.Errorf("could not store ref: %v", err)
continue
}
// @@TODO: check stored refHash against the one we requested

ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

h.announceRefs(ctx, []types.RefID{
{HashAlg: types.SHA1, Hash: sha1Hash},
{HashAlg: types.SHA3, Hash: sha3Hash},
})
return
}

```

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: 6b3e915822f3dfa8e31c721041d1781c0bdbc7e3

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.