Make sure err never gets masked

Defining err as named return parameter will make sure the variable gets
assigned before returning and thus avoid masking

Docker-DCO-1.1-Signed-off-by: Johannes 'fish' Ziemke <github@freigeist.org> (github: discordianfish)
This commit is contained in:
Johannes 'fish' Ziemke 2014-07-18 16:57:32 +02:00
parent c11660169a
commit 32bc865879
1 changed files with 10 additions and 13 deletions

View File

@ -37,24 +37,16 @@ func SetIptablesChain(c *iptables.Chain) {
chain = c chain = c
} }
func Map(container net.Addr, hostIP net.IP, hostPort int) (net.Addr, error) { func Map(container net.Addr, hostIP net.IP, hostPort int) (host net.Addr, err error) {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
var ( var (
m *mapping m *mapping
err error
proto string proto string
allocatedHostPort int allocatedHostPort int
) )
// release the port on any error during return.
defer func() {
if err != nil {
portallocator.ReleasePort(hostIP, proto, allocatedHostPort)
}
}()
switch container.(type) { switch container.(type) {
case *net.TCPAddr: case *net.TCPAddr:
proto = "tcp" proto = "tcp"
@ -77,14 +69,19 @@ func Map(container net.Addr, hostIP net.IP, hostPort int) (net.Addr, error) {
container: container, container: container,
} }
default: default:
err = ErrUnknownBackendAddressType return nil, ErrUnknownBackendAddressType
return nil, err
} }
// release the allocated port on any further error during return.
defer func() {
if err != nil {
portallocator.ReleasePort(hostIP, proto, allocatedHostPort)
}
}()
key := getKey(m.host) key := getKey(m.host)
if _, exists := currentMappings[key]; exists { if _, exists := currentMappings[key]; exists {
err = ErrPortMappedForIP return nil, ErrPortMappedForIP
return nil, err
} }
containerIP, containerPort := getIPAndPort(m.container) containerIP, containerPort := getIPAndPort(m.container)