prepareStdio: use named return for err

This avoids issues with the various deferred error handlers in the event that
`err` is shadowed or named differently, which this function currently avoids
but which is an easy trap to fall into.

Since named return values are all or nothing we need to name the waitGroup too
and adjust the code to suite.

Thanks to Aaron Lehmann for the suggestion, see also
https://github.com/docker/swarmkit/pull/1965#discussion_r118137410

Signed-off-by: Ian Campbell <ian.campbell@docker.com>
This commit is contained in:
Ian Campbell 2017-05-24 15:24:44 +01:00
parent c257deb1a0
commit d7deba03a2

View File

@ -21,8 +21,8 @@ import (
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
) )
func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup, error) { func prepareStdio(stdin, stdout, stderr string, console bool) (wg *sync.WaitGroup, err error) {
var wg sync.WaitGroup wg = &sync.WaitGroup{}
ctx := gocontext.Background() ctx := gocontext.Background()
f, err := fifo.OpenFifo(ctx, stdin, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700) f, err := fifo.OpenFifo(ctx, stdin, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
@ -73,7 +73,7 @@ func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup,
}(f) }(f)
} }
return &wg, nil return wg, nil
} }
func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) { func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {