github-vet / github-vet/rangeloop-pointer-findings
jgoerzen/nncp: src/go.cypherpunks.ru/nncp/v5/cmd/nncp-xfer/main.go; 129 LoC
- 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#L262-L390)
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.
> function call which takes a reference to nodeId at line 314 may start a goroutine
[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#L262-L390)
Click here to show the 129 line(s) of Go which triggered the analyzer.
```go
for nodeId, _ := range ctx.Neigh {
sds["node"] = nodeId
if nodeOnly != nil && nodeId != *nodeOnly.Id {
ctx.LogD("nncp-xfer", sds, "skip")
continue
}
dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
if err != nil {
continue
}
nodePath := filepath.Join(flag.Arg(0), nodeId.String())
sds["dir"] = nodePath
_, err = os.Stat(nodePath)
if err != nil {
if os.IsNotExist(err) {
ctx.LogD("nncp-xfer", sds, "does not exist")
if !*mkdir {
ctx.UnlockDir(dirLock)
continue
}
if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", sds, err, "mkdir")
isBad = true
continue
}
} else {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", sds, err, "stat")
isBad = true
continue
}
}
dstPath := filepath.Join(nodePath, ctx.SelfId.String())
sds["dir"] = dstPath
_, err = os.Stat(dstPath)
if err != nil {
if os.IsNotExist(err) {
if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", sds, err, "mkdir")
isBad = true
continue
}
} else {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", sds, err, "stat")
isBad = true
continue
}
}
delete(sds, "dir")
for job := range ctx.Jobs(&nodeId, nncp.TTx) {
pktName := filepath.Base(job.Fd.Name())
sds["pkt"] = pktName
if job.PktEnc.Nice > nice {
ctx.LogD("nncp-xfer", sds, "too nice")
job.Fd.Close() // #nosec G104
continue
}
if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
ctx.LogD("nncp-xfer", sds, "already exists")
job.Fd.Close() // #nosec G104
continue
}
if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
ctx.LogD("nncp-xfer", sds, "already exists")
job.Fd.Close() // #nosec G104
continue
}
tmp, err := nncp.TempFile(dstPath, "xfer")
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "mktemp")
job.Fd.Close() // #nosec G104
isBad = true
break
}
sds["tmp"] = tmp.Name()
ctx.LogD("nncp-xfer", sds, "created")
bufW := bufio.NewWriter(tmp)
copied, err := nncp.CopyProgressed(
bufW, bufio.NewReader(job.Fd), "Tx",
nncp.SdsAdd(sds, nncp.SDS{"fullsize": job.Size}),
ctx.ShowPrgrs,
)
job.Fd.Close() // #nosec G104
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "copy")
tmp.Close() // #nosec G104
isBad = true
continue
}
if err = bufW.Flush(); err != nil {
tmp.Close() // #nosec G104
ctx.LogE("nncp-xfer", sds, err, "flush")
isBad = true
continue
}
if err = tmp.Sync(); err != nil {
tmp.Close() // #nosec G104
ctx.LogE("nncp-xfer", sds, err, "sync")
isBad = true
continue
}
if err = tmp.Close(); err != nil {
ctx.LogE("nncp-xfer", sds, err, "sync")
}
if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
ctx.LogE("nncp-xfer", sds, err, "rename")
isBad = true
continue
}
if err = nncp.DirSync(dstPath); err != nil {
ctx.LogE("nncp-xfer", sds, err, "sync")
isBad = true
continue
}
os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
delete(sds, "tmp")
ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"size": copied}), "")
if !*keep {
if err = os.Remove(job.Fd.Name()); err != nil {
ctx.LogE("nncp-xfer", sds, err, "remove")
isBad = true
}
}
}
ctx.UnlockDir(dirLock)
}
```
Click here to show extra information the analyzer produced.
```
The following graphviz dot graph describes paths through the callgraph that could lead to a function calling a goroutine:
digraph G {
"(Jobs, 2)" -> {}
}
```
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.