linter: enable nilerr

A number of cases looked suspicious, so I marked them with `FIXME`s to
leave some breadcrumbs.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2022-03-21 14:49:47 +01:00
parent 081e091437
commit 0f12b6fe55
21 changed files with 47 additions and 33 deletions

View File

@ -62,7 +62,6 @@ linters:
- ireturn - ireturn
- varnamelen - varnamelen
- maintidx - maintidx
- nilerr
- nilnil - nilnil
linters-settings: linters-settings:
errcheck: errcheck:

View File

@ -449,7 +449,7 @@ func resolvePathOnDestinationContainer(container string, containerPath string, i
containerInfo, err = registry.ContainerEngine().ContainerStat(registry.GetContext(), container, containerPath) containerInfo, err = registry.ContainerEngine().ContainerStat(registry.GetContext(), container, containerPath)
if err == nil { if err == nil {
baseName = filepath.Base(containerInfo.LinkTarget) baseName = filepath.Base(containerInfo.LinkTarget)
return return // nolint: nilerr
} }
if strings.HasSuffix(containerPath, "/") { if strings.HasSuffix(containerPath, "/") {

View File

@ -264,7 +264,7 @@ func saveToRemote(image, localFile string, tag string, uri *urlP.URL, iden strin
run := podman + " image save " + image + " --format=oci-archive --output=" + remoteFile // run ssh image load of the file copied via scp. Files are reverse in this case... run := podman + " image save " + image + " --format=oci-archive --output=" + remoteFile // run ssh image load of the file copied via scp. Files are reverse in this case...
_, err = connection.ExecRemoteCommand(dial, run) _, err = connection.ExecRemoteCommand(dial, run)
if err != nil { if err != nil {
return nil return err
} }
n, err := scpD.CopyFrom(dial, remoteFile, localFile) n, err := scpD.CopyFrom(dial, remoteFile, localFile)
connection.ExecRemoteCommand(dial, "rm "+remoteFile) connection.ExecRemoteCommand(dial, "rm "+remoteFile)

View File

@ -171,7 +171,7 @@ func printVerbose(cmd *cobra.Command, reports *entities.SystemDfReport) error {
return err return err
} }
if err := writeTemplate(rpt, hdrs, dfImages); err != nil { if err := writeTemplate(rpt, hdrs, dfImages); err != nil {
return nil return err
} }
fmt.Fprint(rpt.Writer(), "\nContainers space usage:\n\n") fmt.Fprint(rpt.Writer(), "\nContainers space usage:\n\n")
@ -191,7 +191,7 @@ func printVerbose(cmd *cobra.Command, reports *entities.SystemDfReport) error {
return err return err
} }
if err := writeTemplate(rpt, hdrs, dfContainers); err != nil { if err := writeTemplate(rpt, hdrs, dfContainers); err != nil {
return nil return err
} }
fmt.Fprint(rpt.Writer(), "\nLocal Volumes space usage:\n\n") fmt.Fprint(rpt.Writer(), "\nLocal Volumes space usage:\n\n")

View File

@ -366,7 +366,8 @@ func (s *BoltState) GetDBConfig() (*DBConfig, error) {
err = db.View(func(tx *bolt.Tx) error { err = db.View(func(tx *bolt.Tx) error {
configBucket, err := getRuntimeConfigBucket(tx) configBucket, err := getRuntimeConfigBucket(tx)
if err != nil { if err != nil {
return nil // FIXME: this error should probably be returned
return nil // nolint: nilerr
} }
// Some of these may be nil // Some of these may be nil

View File

@ -2587,7 +2587,7 @@ func (c *Container) generateUserGroupEntry(addedGID int) (string, int, error) {
gid, err := strconv.ParseUint(group, 10, 32) gid, err := strconv.ParseUint(group, 10, 32)
if err != nil { if err != nil {
return "", 0, nil return "", 0, nil // nolint: nilerr
} }
if addedGID != 0 && addedGID == int(gid) { if addedGID != 0 && addedGID == int(gid) {
@ -2740,7 +2740,8 @@ func (c *Container) generateUserPasswdEntry(addedUID int) (string, int, int, err
// If a non numeric User, then don't generate passwd // If a non numeric User, then don't generate passwd
uid, err := strconv.ParseUint(userspec, 10, 32) uid, err := strconv.ParseUint(userspec, 10, 32)
if err != nil { if err != nil {
return "", 0, 0, nil // FIXME: this error should probably be returned
return "", 0, 0, nil // nolint: nilerr
} }
if addedUID != 0 && int(uid) == addedUID { if addedUID != 0 && int(uid) == addedUID {

View File

@ -750,7 +750,7 @@ func openControlFile(ctr *Container, parentDir string) (*os.File, error) {
for i := 0; i < 600; i++ { for i := 0; i < 600; i++ {
controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY|unix.O_NONBLOCK, 0) controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY|unix.O_NONBLOCK, 0)
if err == nil { if err == nil {
return controlFile, err return controlFile, nil
} }
if !isRetryable(err) { if !isRetryable(err) {
return nil, errors.Wrapf(err, "could not open ctl file for terminal resize for container %s", ctr.ID()) return nil, errors.Wrapf(err, "could not open ctl file for terminal resize for container %s", ctr.ID())
@ -1015,7 +1015,8 @@ func (r *ConmonOCIRuntime) getLogTag(ctr *Container) (string, error) {
} }
data, err := ctr.inspectLocked(false) data, err := ctr.inspectLocked(false)
if err != nil { if err != nil {
return "", nil // FIXME: this error should probably be returned
return "", nil // nolint: nilerr
} }
tmpl, err := template.New("container").Parse(logTag) tmpl, err := template.New("container").Parse(logTag)
if err != nil { if err != nil {

View File

@ -63,7 +63,7 @@ func IsRegistryReference(name string) error {
imageRef, err := alltransports.ParseImageName(name) imageRef, err := alltransports.ParseImageName(name)
if err != nil { if err != nil {
// No supported transport -> assume a docker-stype reference. // No supported transport -> assume a docker-stype reference.
return nil return nil // nolint: nilerr
} }
if imageRef.Transport().Name() == docker.Transport.Name() { if imageRef.Transport().Name() == docker.Transport.Name() {
return nil return nil

View File

@ -279,7 +279,7 @@ func DemuxFrame(r io.Reader, buffer []byte, length int) (frame []byte, err error
n, err := io.ReadFull(r, buffer[0:length]) n, err := io.ReadFull(r, buffer[0:length])
if err != nil { if err != nil {
return nil, nil return nil, err
} }
if n < length { if n < length {
err = io.ErrUnexpectedEOF err = io.ErrUnexpectedEOF

View File

@ -211,7 +211,7 @@ func (b *bindingTest) RunTopContainer(containerName *string, podName *string) (s
} }
ctr, err := containers.CreateWithSpec(b.conn, s, nil) ctr, err := containers.CreateWithSpec(b.conn, s, nil)
if err != nil { if err != nil {
return "", nil return "", err
} }
err = containers.Start(b.conn, ctr.ID, nil) err = containers.Start(b.conn, ctr.ID, nil)
if err != nil { if err != nil {

View File

@ -99,13 +99,12 @@ func CRRemoveDeletedFiles(id, baseDirectory, containerRootDirectory string) erro
// root file system changes on top of containerRootDirectory // root file system changes on top of containerRootDirectory
func CRApplyRootFsDiffTar(baseDirectory, containerRootDirectory string) error { func CRApplyRootFsDiffTar(baseDirectory, containerRootDirectory string) error {
rootfsDiffPath := filepath.Join(baseDirectory, metadata.RootFsDiffTar) rootfsDiffPath := filepath.Join(baseDirectory, metadata.RootFsDiffTar)
if _, err := os.Stat(rootfsDiffPath); err != nil { // Only do this if a rootfs-diff.tar actually exists
// Only do this if a rootfs-diff.tar actually exists
return nil
}
rootfsDiffFile, err := os.Open(rootfsDiffPath) rootfsDiffFile, err := os.Open(rootfsDiffPath)
if err != nil { if err != nil {
if os.IsNotExist(err) {
return nil
}
return errors.Wrap(err, "failed to open root file-system diff file") return errors.Wrap(err, "failed to open root file-system diff file")
} }
defer rootfsDiffFile.Close() defer rootfsDiffFile.Close()

View File

@ -213,8 +213,10 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
for _, val := range filterValues { for _, val := range filterValues {
net, err := r.Network().NetworkInspect(val) net, err := r.Network().NetworkInspect(val)
if err != nil { if err != nil {
// ignore not found errors if errors.Cause(err) == define.ErrNoSuchNetwork {
break continue
}
return nil, err
} }
inputNetNames = append(inputNetNames, net.Name) inputNetNames = append(inputNetNames, net.Name)
} }

View File

@ -131,8 +131,10 @@ func GeneratePodFilterFunc(filter string, filterValues []string, r *libpod.Runti
for _, val := range filterValues { for _, val := range filterValues {
net, err := r.Network().NetworkInspect(val) net, err := r.Network().NetworkInspect(val)
if err != nil { if err != nil {
// ignore not found errors if errors.Cause(err) == define.ErrNoSuchNetwork {
break continue
}
return nil, err
} }
inputNetNames = append(inputNetNames, net.Name) inputNetNames = append(inputNetNames, net.Name)
} }

View File

@ -171,7 +171,8 @@ func (ic *ContainerEngine) VolumeMounted(ctx context.Context, nameOrID string) (
} }
mountCount, err := vol.MountCount() mountCount, err := vol.MountCount()
if err != nil { if err != nil {
return &entities.BoolReport{Value: false}, nil // FIXME: this error should probably be returned
return &entities.BoolReport{Value: false}, nil // nolint: nilerr
} }
if mountCount > 0 { if mountCount > 0 {
return &entities.BoolReport{Value: true}, nil return &entities.BoolReport{Value: true}, nil

View File

@ -840,7 +840,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
if eventsErr != nil || lastEvent == nil { if eventsErr != nil || lastEvent == nil {
logrus.Errorf("Cannot get exit code: %v", err) logrus.Errorf("Cannot get exit code: %v", err)
report.ExitCode = define.ExecErrorCodeNotFound report.ExitCode = define.ExecErrorCodeNotFound
return &report, nil // compat with local client return &report, nil // nolint: nilerr
} }
report.ExitCode = lastEvent.ContainerExitCode report.ExitCode = lastEvent.ContainerExitCode

View File

@ -28,7 +28,7 @@ func JoinErrors(errs []error) error {
finalErr := multiE.ErrorOrNil() finalErr := multiE.ErrorOrNil()
if finalErr == nil { if finalErr == nil {
return finalErr return nil
} }
return errors.New(strings.TrimSpace(finalErr.Error())) return errors.New(strings.TrimSpace(finalErr.Error()))
} }

View File

@ -59,7 +59,10 @@ func (f FedoraDownload) Get() *Download {
func (f FedoraDownload) HasUsableCache() (bool, error) { func (f FedoraDownload) HasUsableCache() (bool, error) {
info, err := os.Stat(f.LocalPath) info, err := os.Stat(f.LocalPath)
if err != nil { if err != nil {
return false, nil if os.IsNotExist(err) {
return false, nil
}
return false, err
} }
return info.Size() == f.Size, nil return info.Size() == f.Size, nil
} }

View File

@ -626,7 +626,8 @@ func (v *MachineVM) Stop(name string, _ machine.StopOptions) error {
} }
if err := qmpMonitor.Disconnect(); err != nil { if err := qmpMonitor.Disconnect(); err != nil {
return nil // FIXME: this error should probably be returned
return nil // nolint: nilerr
} }
disconnected = true disconnected = true
@ -755,7 +756,8 @@ func (v *MachineVM) isRunning() (bool, error) {
// Check if we can dial it // Check if we can dial it
monitor, err := qmp.NewSocketMonitor(v.QMPMonitor.Network, v.QMPMonitor.Address, v.QMPMonitor.Timeout) monitor, err := qmp.NewSocketMonitor(v.QMPMonitor.Network, v.QMPMonitor.Address, v.QMPMonitor.Timeout)
if err != nil { if err != nil {
return false, nil // FIXME: this error should probably be returned
return false, nil // nolint: nilerr
} }
if err := monitor.Connect(); err != nil { if err := monitor.Connect(); err != nil {
return false, err return false, err

View File

@ -16,12 +16,15 @@ import (
// file. // file.
func TryJoinPauseProcess(pausePidPath string) (bool, int, error) { func TryJoinPauseProcess(pausePidPath string) (bool, int, error) {
if _, err := os.Stat(pausePidPath); err != nil { if _, err := os.Stat(pausePidPath); err != nil {
return false, -1, nil if os.IsNotExist(err) {
return false, -1, nil
}
return false, -1, err
} }
became, ret, err := TryJoinFromFilePaths("", false, []string{pausePidPath}) became, ret, err := TryJoinFromFilePaths("", false, []string{pausePidPath})
if err == nil { if err == nil {
return became, ret, err return became, ret, nil
} }
// It could not join the pause process, let's lock the file before trying to delete it. // It could not join the pause process, let's lock the file before trying to delete it.
@ -46,7 +49,7 @@ func TryJoinPauseProcess(pausePidPath string) (bool, int, error) {
if err != nil { if err != nil {
// It is still failing. We can safely remove it. // It is still failing. We can safely remove it.
os.Remove(pausePidPath) os.Remove(pausePidPath)
return false, -1, nil return false, -1, nil // nolint: nilerr
} }
return became, ret, err return became, ret, err
} }

View File

@ -727,7 +727,7 @@ func SocketPath() (string, error) {
func LookupUser(name string) (*user.User, error) { func LookupUser(name string) (*user.User, error) {
// Assume UID look up first, if it fails lookup by username // Assume UID look up first, if it fails lookup by username
if u, err := user.LookupId(name); err == nil { if u, err := user.LookupId(name); err == nil {
return u, err return u, nil
} }
return user.Lookup(name) return user.Lookup(name)
} }

View File

@ -184,7 +184,7 @@ func (matcher *ValidJSONMatcher) Match(actual interface{}) (success bool, err er
var i interface{} var i interface{}
if err := json.Unmarshal([]byte(s), &i); err != nil { if err := json.Unmarshal([]byte(s), &i); err != nil {
return false, nil return false, err
} }
return true, nil return true, nil
} }