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

hyperhq/hyperd: networking/portmapping/host_portmapping_linux.go; 52 LoC

Open
#16,252 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 [hyperhq/hyperd](https://www.github.com/hyperhq/hyperd) at [networking/portmapping/host_portmapping_linux.go](https://github.com/hyperhq/hyperd/blob/8a7e67dc33bbccdfc92b5e2a908bbdb1c5d9b956/networking/portmapping/host_portmapping_linux.go#L152-L203)

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 m used in defer or goroutine at line 188

[Click here to see the code in its original context.](https://github.com/hyperhq/hyperd/blob/8a7e67dc33bbccdfc92b5e2a908bbdb1c5d9b956/networking/portmapping/host_portmapping_linux.go#L152-L203)

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

```go
for _, m := range maps {

natArgs, filterArgs, err := generateIptablesArgs(containerip, m)
if err != nil {
revert = true
return err
}

//check if this rule has already existed
if iptables.PortMapExists("HYPER", natArgs) {
continue
}

if iptables.PortMapUsed("HYPER", m.Protocol, m.FromPorts.Begin, m.FromPorts.End) {
revert = true
return fmt.Errorf("Host port %v has aleady been used", m.FromPorts)
}

err = parseRawResultOnHyper(iptables.Raw(append([]string{"-t", "nat", "-I", "HYPER"}, natArgs...)...))
if err != nil {
revert = true
return fmt.Errorf("Unable to setup NAT rule in HYPER chain: %s", err)
}
revertRules = append(revertRules, append([]string{"-t", "nat", "-D", "HYPER"}, natArgs...))

if err = parseRawResultOnHyper(iptables.Raw(append([]string{"-I", "HYPER"}, filterArgs...)...)); err != nil {
revert = true
return fmt.Errorf("Unable to setup FILTER rule in HYPER chain: %s", err)
}
revertRules = append(revertRules, append([]string{"-D", "HYPER"}, filterArgs...))

i := m.FromPorts.Begin
j := m.ToPorts.Begin
defer func() {
if err != nil {
i--
for i >= m.FromPorts.Begin {
PortMapper.ReleaseMap(m.Protocol, i)
i--
}
}
}()

for i <= m.FromPorts.End {
if err = PortMapper.AllocateMap(m.Protocol, i, containerip, j); err != nil {
revert = true
return err
}
i++
j++
}
}

```

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: 8a7e67dc33bbccdfc92b5e2a908bbdb1c5d9b956

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.