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

jgoerzen/nncp: src/go.cypherpunks.ru/nncp/v5/cmd/nncp-xfer/main.go; 84 LoC

Open
#15,624 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 [jgoerzen/nncp](https://www.github.com/jgoerzen/nncp) at [src/go.cypherpunks.ru/nncp/v5/cmd/nncp-xfer/main.go](https://github.com/jgoerzen/nncp/blob/46be2c9c706cd824fe78404bace49c88ec236bdd/src/go.cypherpunks.ru/nncp/v5/cmd/nncp-xfer/main.go#L168-L251)

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 fiInt used in defer or goroutine at line 212

[Click here to see the code in its original context.](https://github.com/jgoerzen/nncp/blob/46be2c9c706cd824fe78404bace49c88ec236bdd/src/go.cypherpunks.ru/nncp/v5/cmd/nncp-xfer/main.go#L168-L251)

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

```go
for _, fiInt := range fisInt {
if !fi.IsDir() {
continue
}
// Check that it is valid Base32 encoding
if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
continue
}
filename := filepath.Join(dir.Name(), fiInt.Name())
sds["file"] = filename
delete(sds, "size")
fd, err := os.Open(filename)
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "open")
isBad = true
continue
}
var pktEnc nncp.PktEnc
_, err = xdr.Unmarshal(fd, &pktEnc)
if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
ctx.LogD("nncp-xfer", sds, "is not a packet")
fd.Close() // #nosec G104
continue
}
if pktEnc.Nice > nice {
ctx.LogD("nncp-xfer", sds, "too nice")
fd.Close() // #nosec G104
continue
}
sds["size"] = fiInt.Size()
if !ctx.IsEnoughSpace(fiInt.Size()) {
ctx.LogE("nncp-xfer", sds, errors.New("is not enough space"), "")
fd.Close() // #nosec G104
continue
}
if _, err = fd.Seek(0, 0); err != nil {
log.Fatalln(err)
}
tmp, err := ctx.NewTmpFileWHash()
if err != nil {
log.Fatalln(err)
}
r, w := io.Pipe()
go func() {
_, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
if err == nil {
err = w.Close()
}
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "copy")
w.CloseWithError(err) // #nosec G104
}
}()
if _, err = nncp.CopyProgressed(
tmp.W, r, "Rx",
nncp.SdsAdd(sds, nncp.SDS{
"pkt": filename,
"fullsize": sds["size"],
}),
ctx.ShowPrgrs,
); err != nil {
ctx.LogE("nncp-xfer", sds, err, "copy")
isBad = true
}
fd.Close() // #nosec G104
if isBad {
tmp.Cancel()
continue
}
if err = tmp.Commit(filepath.Join(
ctx.Spool,
nodeId.String(),
string(nncp.TRx),
)); err != nil {
log.Fatalln(err)
}
ctx.LogI("nncp-xfer", sds, "")
if !*keep {
if err = os.Remove(filename); err != nil {
ctx.LogE("nncp-xfer", sds, err, "remove")
isBad = true
}
}
}

```

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: 46be2c9c706cd824fe78404bace49c88ec236bdd

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.