github-vet / github-vet/rangeloop-pointer-findings
Crazykev/hyper: daemon/vm.go; 103 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [Crazykev/hyper](https://www.github.com/Crazykev/hyper) at [daemon/vm.go](https://github.com/Crazykev/hyper/blob/bb1c29ccb9961a1faf9fc243f27317b23d7eb223/daemon/vm.go#L153-L255)
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 mypod used in defer or goroutine at line 199
[Click here to see the code in its original context.](https://github.com/Crazykev/hyper/blob/bb1c29ccb9961a1faf9fc243f27317b23d7eb223/daemon/vm.go#L153-L255)
Click here to show the 103 line(s) of Go which triggered the analyzer.
```go
for _, mypod := range daemon.podList {
if mypod.Vm == "" {
continue
}
data, err := daemon.GetPodByName(mypod.Id)
if err != nil {
continue
}
userPod, err := pod.ProcessPodBytes(data)
if err != nil {
continue
}
glog.V(1).Infof("Associate the POD(%s) with VM(%s)", mypod.Id, mypod.Vm)
var (
qemuPodEvent = make(chan hypervisor.VmEvent, 128)
qemuStatus = make(chan *types.QemuResponse, 128)
subQemuStatus = make(chan *types.QemuResponse, 128)
)
data, err = daemon.GetVmData(mypod.Vm)
if err != nil {
continue
}
glog.V(1).Infof("The data for vm(%s) is %v", mypod.Vm, data)
go hypervisor.VmAssociate(hypervisorDriver, mypod.Vm, qemuPodEvent,
qemuStatus, mypod.Wg, data)
ass := <-qemuStatus
if ass.Code != types.E_OK {
glog.Errorf("cannot associate with vm: %s, error status %d (%s)", mypod.Vm, ass.Code, ass.Cause)
return errors.New("load vm status failed")
}
if err := daemon.SetQemuChan(mypod.Vm, qemuPodEvent, qemuStatus, subQemuStatus); err != nil {
glog.V(1).Infof("SetQemuChan error: %s", err.Error())
return err
}
vm := &Vm{
Id: mypod.Vm,
Pod: mypod,
Status: types.S_VM_ASSOCIATED,
Cpu: userPod.Resource.Vcpu,
Mem: userPod.Resource.Memory,
}
daemon.AddVm(vm)
daemon.SetContainerStatus(mypod.Id, types.S_POD_RUNNING)
mypod.Status = types.S_POD_RUNNING
go func(interface{}) {
for {
podId := mypod.Id
qemuResponse := <-qemuStatus
subQemuStatus <- qemuResponse
if qemuResponse.Code == types.E_POD_FINISHED {
data := qemuResponse.Data.([]uint32)
daemon.SetPodContainerStatus(podId, data)
} else if qemuResponse.Code == types.E_VM_SHUTDOWN {
if daemon.podList[mypod.Id].Status == types.S_POD_RUNNING {
daemon.podList[mypod.Id].Status = types.S_POD_SUCCEEDED
daemon.SetContainerStatus(podId, types.S_POD_SUCCEEDED)
}
daemon.podList[mypod.Id].Vm = ""
daemon.RemoveVm(mypod.Vm)
daemon.DeleteQemuChan(mypod.Vm)
if mypod.Type == "kubernetes" {
switch mypod.Status {
case types.S_POD_SUCCEEDED:
if mypod.RestartPolicy == "always" {
daemon.RestartPod(mypod)
} else {
daemon.DeletePodFromDB(podId)
for _, c := range mypod.Containers {
glog.V(1).Infof("Ready to rm container: %s", c.Id)
if _, _, err = daemon.dockerCli.SendCmdDelete(c.Id); err != nil {
glog.V(1).Infof("Error to rm container: %s", err.Error())
}
}
// daemon.RemovePod(podId)
daemon.DeletePodContainerFromDB(podId)
daemon.DeleteVolumeId(podId)
}
break
case types.S_POD_FAILED:
if mypod.RestartPolicy != "never" {
daemon.RestartPod(mypod)
} else {
daemon.DeletePodFromDB(podId)
for _, c := range mypod.Containers {
glog.V(1).Infof("Ready to rm container: %s", c.Id)
if _, _, err = daemon.dockerCli.SendCmdDelete(c.Id); err != nil {
glog.V(1).Infof("Error to rm container: %s", err.Error())
}
}
// daemon.RemovePod(podId)
daemon.DeletePodContainerFromDB(podId)
daemon.DeleteVolumeId(podId)
}
break
default:
break
}
}
break
}
}
}(subQemuStatus)
}
```
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: bb1c29ccb9961a1faf9fc243f27317b23d7eb223
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.