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

alexmavr/swarm-nbt: udp.go; 70 LoC

Open
#11,327 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 [alexmavr/swarm-nbt](https://www.github.com/alexmavr/swarm-nbt) at [udp.go](https://github.com/alexmavr/swarm-nbt/blob/595e39a23f41f0676f3ab806de0d7d52a2e2ce76/udp.go#L57-L126)

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 target used in defer or goroutine at line 89

[Click here to see the code in its original context.](https://github.com/alexmavr/swarm-nbt/blob/595e39a23f41f0676f3ab806de0d7d52a2e2ce76/udp.go#L57-L126)

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

```go
for _, target := range p.Targets {
// Capture the start time for the RTT measurement after a UDP socket is obtained
startTime := time.Now()

// Open a UDP socket
conn, err := net.DialUDP("udp", nil, target.Addr)
if err != nil {
log.Errorf("unable to dial UDP target %s: %s", target.Addr.IP.String(), err)
return
}
target.Conn = conn
defer target.Conn.Close()

// Generate a random UUID for the packet ID
newUUID := uuid.NewV4().String()
payload := fmt.Sprintf("%s\t%s", newUUID, p.NodeAddr.IP.String())

log.Infof("UDP: sending SYN against %s, UUID: %s", target.Addr.IP.String(), newUUID)
// Send the UDP packet
_, err = target.Conn.Write([]byte(payload))
if err != nil {
log.Errorf("unable to write UDP packet: %s", err)
return
}

// The UUID of the packet is transfered through the uuidChan
uuidChan := make(chan string)

// In a goroutine, attempt to read the ACK response from the server
var terminated bool = false
go func(uuidChan chan string, terminated *bool) {
var buf [1024]byte
rlen, _, err := target.Conn.ReadFromUDP(buf[:])
if err != nil {
if *terminated {
// Don't record an error if this was caused due to a timeout
return
}
log.Errorf("UDP Read error %s:", err)
udpPacketLoss.WithLabelValues(target.Addr.IP.String(), formatManagersLabel(p.IsManager, target.IsManager)).Set(1)
udpRTT.WithLabelValues(target.Addr.IP.String(), formatManagersLabel(p.IsManager, target.IsManager)).Set(0)

return
}

// Determine if the received packet is an ACK packet
// from a request sent from this node
payload := string(buf[0:rlen])
payloadParts, ack := isAck(payload)
if !ack {
log.Errorf("Client collected non-ACK packet: %v", payload)
return
}
uuidChan <- payloadParts[1]
}(uuidChan, &terminated)

// Block on reception of the ACK, or a timeout
select {
case uuid := <-uuidChan:
p.ReceivedPacket(uuid, startTime, target)
udpPacketLoss.WithLabelValues(target.Addr.IP.String(), formatManagersLabel(p.IsManager, target.IsManager)).Set(0)
case <-time.Tick(udpClientTimeout):
// The client waits on an ACK timeout
log.Warnf("UDP: Timeout waiting for ACK from %s", target.Addr.IP.String())
udpPacketLoss.WithLabelValues(target.Addr.IP.String(), formatManagersLabel(p.IsManager, target.IsManager)).Set(1)
udpRTT.WithLabelValues(target.Addr.IP.String(), formatManagersLabel(p.IsManager, target.IsManager)).Set(0)
terminated = true
}
target.Conn.Close()
}

```

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: 595e39a23f41f0676f3ab806de0d7d52a2e2ce76

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.