util: handle Close() errors in pidlimit

A new version of gosec insists on handling errors returned by Close():

    [/go/src/github.com/ceph/ceph-csi/internal/util/pidlimit.go:44] - G307 (CWE-): Deferring unsafe method "*os.File" on type "Close" (Confidence: HIGH, Severity: MEDIUM)
      > defer cgroup.Close()

    [/go/src/github.com/ceph/ceph-csi/internal/util/pidlimit.go:78] - G307 (CWE-): Deferring unsafe method "*os.File" on type "Close" (Confidence: HIGH, Severity: MEDIUM)
      > defer f.Close()

    [/go/src/github.com/ceph/ceph-csi/internal/util/pidlimit.go:113] - G307 (CWE-): Deferring unsafe method "*os.File" on type "Close" (Confidence: HIGH, Severity: MEDIUM)
      > defer f.Close()

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-06-30 12:15:23 +02:00 committed by mergify[bot]
parent 30dc83c3bb
commit 1b89f86d51

View File

@ -41,7 +41,7 @@ func getCgroupPidsFile() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
defer cgroup.Close() defer cgroup.Close() // #nosec: error on close is not critical here
scanner := bufio.NewScanner(cgroup) scanner := bufio.NewScanner(cgroup)
var slice string var slice string
@ -75,7 +75,7 @@ func GetPIDLimit() (int, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer f.Close() defer f.Close() // #nosec: error on close is not critical here
maxPidsStr, err := bufio.NewReader(f).ReadString('\n') maxPidsStr, err := bufio.NewReader(f).ReadString('\n')
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
@ -110,12 +110,12 @@ func SetPIDLimit(limit int) error {
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
_, err = f.WriteString(limitStr) _, err = f.WriteString(limitStr)
if err != nil { if err != nil {
f.Close() // #nosec: a write error will be more useful to return
return err return err
} }
return nil return f.Close()
} }