From 627a06ad927edb6196860e0f3dce71896996dad2 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 8 Jun 2021 11:24:00 -0700 Subject: [PATCH] Replace fmt.Errorf w/o %-style to errors.New Using fmt.Errorf for errors that do not have %-style formatting directives is an overkill. Switch to errors.New. Found by git grep fmt.Errorf | grep -v ^vendor | grep -v '%' Signed-off-by: Kir Kolyshkin --- contrib/cmd/recvtty/recvtty.go | 5 +++-- exec.go | 5 +++-- libcontainer/cgroups/utils_test.go | 4 ++-- libcontainer/cgroups/v1_utils.go | 2 +- libcontainer/configs/config_linux.go | 17 ++++++++++++----- libcontainer/container_linux.go | 2 +- libcontainer/factory_linux.go | 15 +++++++++------ libcontainer/generic_error_test.go | 8 ++++---- libcontainer/process.go | 10 ++++++---- libcontainer/restored_process.go | 10 +++++----- libcontainer/specconv/spec_linux.go | 6 +++--- libcontainer/state_linux.go | 7 ++++--- libcontainer/user/user.go | 8 ++++---- 13 files changed, 57 insertions(+), 42 deletions(-) diff --git a/contrib/cmd/recvtty/recvtty.go b/contrib/cmd/recvtty/recvtty.go index 777b3f1cc..14f711548 100644 --- a/contrib/cmd/recvtty/recvtty.go +++ b/contrib/cmd/recvtty/recvtty.go @@ -17,6 +17,7 @@ package main import ( + "errors" "fmt" "io" "io/ioutil" @@ -88,7 +89,7 @@ func handleSingle(path string, noStdin bool) error { // Get the fd of the connection. unixconn, ok := conn.(*net.UnixConn) if !ok { - return fmt.Errorf("failed to cast to unixconn") + return errors.New("failed to cast to unixconn") } socket, err := unixconn.File() @@ -217,7 +218,7 @@ func main() { app.Action = func(ctx *cli.Context) error { args := ctx.Args() if len(args) != 1 { - return fmt.Errorf("need to specify a single socket path") + return errors.New("need to specify a single socket path") } path := ctx.Args()[0] diff --git a/exec.go b/exec.go index 7514b58e1..94346e062 100644 --- a/exec.go +++ b/exec.go @@ -4,6 +4,7 @@ package main import ( "encoding/json" + "errors" "fmt" "os" "strconv" @@ -115,11 +116,11 @@ func execProcess(context *cli.Context) (int, error) { return -1, err } if status == libcontainer.Stopped { - return -1, fmt.Errorf("cannot exec a container that has stopped") + return -1, errors.New("cannot exec a container that has stopped") } path := context.String("process") if path == "" && len(context.Args()) == 1 { - return -1, fmt.Errorf("process args cannot be empty") + return -1, errors.New("process args cannot be empty") } detach := context.Bool("detach") state, err := container.State() diff --git a/libcontainer/cgroups/utils_test.go b/libcontainer/cgroups/utils_test.go index d0de02b25..816bdc833 100644 --- a/libcontainer/cgroups/utils_test.go +++ b/libcontainer/cgroups/utils_test.go @@ -4,7 +4,7 @@ package cgroups import ( "bytes" - "fmt" + "errors" "reflect" "strings" "testing" @@ -372,7 +372,7 @@ func TestParseCgroupString(t *testing.T) { }, { input: `malformed input`, - expectedError: fmt.Errorf(`invalid cgroup entry: must contain at least two colons: malformed input`), + expectedError: errors.New(`invalid cgroup entry: must contain at least two colons: malformed input`), }, } diff --git a/libcontainer/cgroups/v1_utils.go b/libcontainer/cgroups/v1_utils.go index 95ec9dff0..1b507f7c3 100644 --- a/libcontainer/cgroups/v1_utils.go +++ b/libcontainer/cgroups/v1_utils.go @@ -154,7 +154,7 @@ func findCgroupMountpointAndRootFromMI(mounts []*mountinfo.Info, cgroupPath, sub func (m Mount) GetOwnCgroup(cgroups map[string]string) (string, error) { if len(m.Subsystems) == 0 { - return "", fmt.Errorf("no subsystem for mount") + return "", errors.New("no subsystem for mount") } return getControllerPath(m.Subsystems[0], cgroups) diff --git a/libcontainer/configs/config_linux.go b/libcontainer/configs/config_linux.go index 07da10804..8c02848b7 100644 --- a/libcontainer/configs/config_linux.go +++ b/libcontainer/configs/config_linux.go @@ -1,17 +1,24 @@ package configs -import "fmt" +import "errors" + +var ( + errNoUIDMap = errors.New("User namespaces enabled, but no uid mappings found.") + errNoUserMap = errors.New("User namespaces enabled, but no user mapping found.") + errNoGIDMap = errors.New("User namespaces enabled, but no gid mappings found.") + errNoGroupMap = errors.New("User namespaces enabled, but no group mapping found.") +) // HostUID gets the translated uid for the process on host which could be // different when user namespaces are enabled. func (c Config) HostUID(containerId int) (int, error) { if c.Namespaces.Contains(NEWUSER) { if c.UidMappings == nil { - return -1, fmt.Errorf("User namespaces enabled, but no uid mappings found.") + return -1, errNoUIDMap } id, found := c.hostIDFromMapping(containerId, c.UidMappings) if !found { - return -1, fmt.Errorf("User namespaces enabled, but no user mapping found.") + return -1, errNoUserMap } return id, nil } @@ -30,11 +37,11 @@ func (c Config) HostRootUID() (int, error) { func (c Config) HostGID(containerId int) (int, error) { if c.Namespaces.Contains(NEWUSER) { if c.GidMappings == nil { - return -1, fmt.Errorf("User namespaces enabled, but no gid mappings found.") + return -1, errNoGIDMap } id, found := c.hostIDFromMapping(containerId, c.GidMappings) if !found { - return -1, fmt.Errorf("User namespaces enabled, but no group mapping found.") + return -1, errNoGroupMap } return id, nil } diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 6ce1854f6..31f02df1e 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -666,7 +666,7 @@ func (c *linuxContainer) Resume() error { return err } if status != Paused { - return newGenericError(fmt.Errorf("container not paused"), ContainerNotPaused) + return newGenericError(errors.New("container not paused"), ContainerNotPaused) } if err := c.cgroupManager.Freeze(configs.Thawed); err != nil { return err diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index dbd410b88..7f27a0519 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -31,7 +31,10 @@ const ( execFifoFilename = "exec.fifo" ) -var idRegex = regexp.MustCompile(`^[\w+-\.]+$`) +var ( + idRegex = regexp.MustCompile(`^[\w+-\.]+$`) + errNoSystemd = errors.New("systemd not running on this host, can't use systemd as cgroups manager") +) // InitArgs returns an options func to configure a LinuxFactory with the // provided init binary path and arguments. @@ -80,7 +83,7 @@ func systemdCgroupV2(l *LinuxFactory, rootless bool) error { // containers that use systemd to create and manage cgroups. func SystemdCgroups(l *LinuxFactory) error { if !systemd.IsRunningSystemd() { - return fmt.Errorf("systemd not running on this host, can't use systemd as cgroups manager") + return errNoSystemd } if cgroups.IsCgroup2UnifiedMode() { @@ -97,11 +100,11 @@ func SystemdCgroups(l *LinuxFactory) error { // RootlessSystemdCgroups is rootless version of SystemdCgroups. func RootlessSystemdCgroups(l *LinuxFactory) error { if !systemd.IsRunningSystemd() { - return fmt.Errorf("systemd not running on this host, can't use systemd as cgroups manager") + return errNoSystemd } if !cgroups.IsCgroup2UnifiedMode() { - return fmt.Errorf("cgroup v2 not enabled on this host, can't use systemd (rootless) as cgroups manager") + return errors.New("cgroup v2 not enabled on this host, can't use systemd (rootless) as cgroups manager") } return systemdCgroupV2(l, true) } @@ -246,7 +249,7 @@ type LinuxFactory struct { func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, error) { if l.Root == "" { - return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid) + return nil, newGenericError(errors.New("invalid root"), ConfigInvalid) } if err := l.validateID(id); err != nil { return nil, err @@ -289,7 +292,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err func (l *LinuxFactory) Load(id string) (Container, error) { if l.Root == "" { - return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid) + return nil, newGenericError(errors.New("invalid root"), ConfigInvalid) } // when load, we need to check id is valid or not. if err := l.validateID(id); err != nil { diff --git a/libcontainer/generic_error_test.go b/libcontainer/generic_error_test.go index 8fbdd4d38..771ab4605 100644 --- a/libcontainer/generic_error_test.go +++ b/libcontainer/generic_error_test.go @@ -1,20 +1,20 @@ package libcontainer import ( - "fmt" + "errors" "io/ioutil" "testing" ) func TestErrorDetail(t *testing.T) { - err := newGenericError(fmt.Errorf("test error"), SystemError) + err := newGenericError(errors.New("test error"), SystemError) if derr := err.Detail(ioutil.Discard); derr != nil { t.Fatal(derr) } } func TestErrorWithCode(t *testing.T) { - err := newGenericError(fmt.Errorf("test error"), SystemError) + err := newGenericError(errors.New("test error"), SystemError) if code := err.Code(); code != SystemError { t.Fatalf("expected err code %q but %q", SystemError, code) } @@ -35,7 +35,7 @@ func TestErrorWithError(t *testing.T) { } for _, v := range cc { - err := newSystemErrorWithCause(fmt.Errorf(v.errmsg), v.cause) + err := newSystemErrorWithCause(errors.New(v.errmsg), v.cause) msg := err.Error() if v.cause == "" && msg != v.errmsg { diff --git a/libcontainer/process.go b/libcontainer/process.go index d3e472a4f..cff1eff4b 100644 --- a/libcontainer/process.go +++ b/libcontainer/process.go @@ -1,7 +1,7 @@ package libcontainer import ( - "fmt" + "errors" "io" "math" "os" @@ -9,6 +9,8 @@ import ( "github.com/opencontainers/runc/libcontainer/configs" ) +var errInvalidProcess = errors.New("invalid process") + type processOperations interface { wait() (*os.ProcessState, error) signal(sig os.Signal) error @@ -84,7 +86,7 @@ type Process struct { // Wait releases any resources associated with the Process func (p Process) Wait() (*os.ProcessState, error) { if p.ops == nil { - return nil, newGenericError(fmt.Errorf("invalid process"), NoProcessOps) + return nil, newGenericError(errInvalidProcess, NoProcessOps) } return p.ops.wait() } @@ -94,7 +96,7 @@ func (p Process) Pid() (int, error) { // math.MinInt32 is returned here, because it's invalid value // for the kill() system call. if p.ops == nil { - return math.MinInt32, newGenericError(fmt.Errorf("invalid process"), NoProcessOps) + return math.MinInt32, newGenericError(errInvalidProcess, NoProcessOps) } return p.ops.pid(), nil } @@ -102,7 +104,7 @@ func (p Process) Pid() (int, error) { // Signal sends a signal to the Process. func (p Process) Signal(sig os.Signal) error { if p.ops == nil { - return newGenericError(fmt.Errorf("invalid process"), NoProcessOps) + return newGenericError(errInvalidProcess, NoProcessOps) } return p.ops.signal(sig) } diff --git a/libcontainer/restored_process.go b/libcontainer/restored_process.go index 34270e64e..a091d1e1a 100644 --- a/libcontainer/restored_process.go +++ b/libcontainer/restored_process.go @@ -3,7 +3,7 @@ package libcontainer import ( - "fmt" + "errors" "os" "os/exec" @@ -31,7 +31,7 @@ type restoredProcess struct { } func (p *restoredProcess) start() error { - return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError) + return newGenericError(errors.New("restored process cannot be started"), SystemError) } func (p *restoredProcess) pid() int { @@ -89,7 +89,7 @@ type nonChildProcess struct { } func (p *nonChildProcess) start() error { - return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError) + return newGenericError(errors.New("restored process cannot be started"), SystemError) } func (p *nonChildProcess) pid() int { @@ -97,11 +97,11 @@ func (p *nonChildProcess) pid() int { } func (p *nonChildProcess) terminate() error { - return newGenericError(fmt.Errorf("restored process cannot be terminated"), SystemError) + return newGenericError(errors.New("restored process cannot be terminated"), SystemError) } func (p *nonChildProcess) wait() (*os.ProcessState, error) { - return nil, newGenericError(fmt.Errorf("restored process cannot be waited on"), SystemError) + return nil, newGenericError(errors.New("restored process cannot be waited on"), SystemError) } func (p *nonChildProcess) startTime() (uint64, error) { diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 8474769c9..01d6f2962 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -216,7 +216,7 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) { } spec := opts.Spec if spec.Root == nil { - return nil, fmt.Errorf("Root must be specified") + return nil, errors.New("Root must be specified") } rootfsPath := spec.Root.Path if !filepath.IsAbs(rootfsPath) { @@ -263,7 +263,7 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) { return nil, fmt.Errorf("rootfsPropagation=%v is not supported", spec.Linux.RootfsPropagation) } if config.NoPivotRoot && (config.RootPropagation&unix.MS_PRIVATE != 0) { - return nil, fmt.Errorf("rootfsPropagation of [r]private is not safe without pivot_root") + return nil, errors.New("rootfsPropagation of [r]private is not safe without pivot_root") } for _, ns := range spec.Linux.Namespaces { @@ -858,7 +858,7 @@ func SetupSeccomp(config *specs.LinuxSeccomp) (*configs.Seccomp, error) { // We don't currently support seccomp flags. if len(config.Flags) != 0 { - return nil, fmt.Errorf("seccomp flags are not yet supported by runc") + return nil, errors.New("seccomp flags are not yet supported by runc") } newConfig := new(configs.Seccomp) diff --git a/libcontainer/state_linux.go b/libcontainer/state_linux.go index 43c040c85..4fa0eab1e 100644 --- a/libcontainer/state_linux.go +++ b/libcontainer/state_linux.go @@ -3,6 +3,7 @@ package libcontainer import ( + "errors" "fmt" "os" "path/filepath" @@ -117,7 +118,7 @@ func (r *runningState) transition(s containerState) error { switch s.(type) { case *stoppedState: if r.c.runType() == Running { - return newGenericError(fmt.Errorf("container still running"), ContainerNotStopped) + return newGenericError(errors.New("container still running"), ContainerNotStopped) } r.c.state = s return nil @@ -132,7 +133,7 @@ func (r *runningState) transition(s containerState) error { func (r *runningState) destroy() error { if r.c.runType() == Running { - return newGenericError(fmt.Errorf("container is not destroyed"), ContainerNotStopped) + return newGenericError(errors.New("container is not destroyed"), ContainerNotStopped) } return destroy(r.c) } @@ -190,7 +191,7 @@ func (p *pausedState) destroy() error { } return destroy(p.c) } - return newGenericError(fmt.Errorf("container is paused"), ContainerPaused) + return newGenericError(errors.New("container is paused"), ContainerPaused) } // restoredState is the same as the running state but also has associated checkpoint diff --git a/libcontainer/user/user.go b/libcontainer/user/user.go index d2c16f7fd..78695801c 100644 --- a/libcontainer/user/user.go +++ b/libcontainer/user/user.go @@ -119,7 +119,7 @@ func ParsePasswdFileFilter(path string, filter func(User) bool) ([]User, error) func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error) { if r == nil { - return nil, fmt.Errorf("nil source for passwd-formatted data") + return nil, errors.New("nil source for passwd-formatted data") } var ( @@ -177,7 +177,7 @@ func ParseGroupFileFilter(path string, filter func(Group) bool) ([]Group, error) func ParseGroupFilter(r io.Reader, filter func(Group) bool) ([]Group, error) { if r == nil { - return nil, fmt.Errorf("nil source for group-formatted data") + return nil, errors.New("nil source for group-formatted data") } var ( @@ -487,7 +487,7 @@ func ParseSubIDFileFilter(path string, filter func(SubID) bool) ([]SubID, error) func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error) { if r == nil { - return nil, fmt.Errorf("nil source for subid-formatted data") + return nil, errors.New("nil source for subid-formatted data") } var ( @@ -540,7 +540,7 @@ func ParseIDMapFileFilter(path string, filter func(IDMap) bool) ([]IDMap, error) func ParseIDMapFilter(r io.Reader, filter func(IDMap) bool) ([]IDMap, error) { if r == nil { - return nil, fmt.Errorf("nil source for idmap-formatted data") + return nil, errors.New("nil source for idmap-formatted data") } var (