github-vet / github-vet/rangeloop-pointer-findings
delthas/autopunch: autopunch-loader/loader.go; 103 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [delthas/autopunch](https://www.github.com/delthas/autopunch) at [autopunch-loader/loader.go](https://github.com/delthas/autopunch/blob/06508116489919eaff1dbe87c6950e517d13d47a/autopunch-loader/loader.go#L653-L755)
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 asset used in defer or goroutine at line 699
[Click here to see the code in its original context.](https://github.com/delthas/autopunch/blob/06508116489919eaff1dbe87c6950e517d13d47a/autopunch-loader/loader.go#L653-L755)
Click here to show the 103 line(s) of Go which triggered the analyzer.
```go
for _, asset := range v.Assets {
if asset.Name != assetName {
continue
}
r, err = httpClient.Get(asset.DownloadUrl)
if err != nil {
dialog("Warning", "Error while downloading update.\nError: "+err.Error(), walk.MsgBoxIconWarning)
return false
}
f, err := ioutil.TempFile("", "")
if err != nil {
r.Body.Close()
dialog("Warning", "Error while creating file for downloading update.\nError: "+err.Error(), walk.MsgBoxIconWarning)
return false
}
pr := progress.NewReader(r.Body)
var done bool
var dw *walk.Dialog
var lb *walk.Label
var pb *walk.ProgressBar
mw.Synchronize(func() {
_, err = Dialog{
AssignTo: &dw,
Title: "autopunch " + version + " (by delthas)",
MinSize: Size{Width: 300, Height: 150},
Size: Size{Width: 400, Height: 200},
Layout: VBox{},
Children: []Widget{
Label{
AssignTo: &lb,
Text: "Update found! Downlading update...\nAutopunch will restart itself automatically when finished.",
},
ProgressBar{
AssignTo: &pb,
MinValue: 0,
MaxValue: 100000,
},
},
}.Run(mw)
if !done {
os.Exit(0) // good enough for now
}
})
go func() {
ctx := context.Background()
progressChan := progress.NewTicker(ctx, pr, asset.Size, 100*time.Millisecond)
for p := range progressChan {
if pb != nil {
pb.Synchronize(func() {
pb.SetValue(int(p.Percent() * float64(pb.MaxValue()) / 100))
})
}
if lb != nil {
text := fmt.Sprintf("Update found! Downlading update, remaining: %v\nAutopunch will restart itself automatically when finished.", p.Remaining().Round(time.Second))
lb.Synchronize(func() {
lb.SetText(text)
})
}
}
}()
_, err = io.Copy(f, pr)
done = true
r.Body.Close()
f.Close()
dw.Close(0)
mw.SetVisible(false)
if err != nil {
dialog("Warning", "Error while downloading update to file.\nError: "+err.Error(), walk.MsgBoxIconWarning)
return false
}
renamePath := ""
for i := 0; i < 10; i++ {
renamePath = filepath.Join(os.TempDir(), "autopunch.old."+strconv.Itoa(1000000000 + rand.Intn(1000000000))[1:]+".exe")
err = os.Rename(autopunchPath, renamePath)
if err == nil {
break
}
}
if err != nil {
dialog("Warning", "Error while updating, when moving current file.\nError: "+err.Error(), walk.MsgBoxIconWarning)
return false
}
err = os.Rename(f.Name(), autopunchPath)
if err != nil {
// try moving the old file back in case of error
_ = os.Rename(renamePath, autopunchPath)
dialog("Warning", "Error while updating, when moving downloaded file.\nError: "+err.Error(), walk.MsgBoxIconWarning)
return false
}
go func() {
cmd := exec.Command(autopunchPath, os.Args[1:]...)
cmd.Env = append(os.Environ(), "AUTOPUNCH_OLD="+renamePath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}()
return 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: 06508116489919eaff1dbe87c6950e517d13d47a
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.