From adbac31d88c413210be7f4884479840c749c69e0 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 8 Jun 2021 20:51:08 -0700 Subject: [PATCH] libct: fix errorlint warning about strconv.NumError This one is tough as errorlint insists on using errors.Is, and the latter is known to not work for Go 1.13 which we still support. So, add a nolint annotation to suppress the warning, and a TODO to address it later. For intelrdt, we can do the same, but it is easier to reuse the very same function from fscommon (note we can't use fscommon for other stuff as it expects cgroupfs). Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fscommon/utils.go | 5 ++++- libcontainer/intelrdt/intelrdt.go | 21 ++------------------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/libcontainer/cgroups/fscommon/utils.go b/libcontainer/cgroups/fscommon/utils.go index 29fd8823c..d7ee48373 100644 --- a/libcontainer/cgroups/fscommon/utils.go +++ b/libcontainer/cgroups/fscommon/utils.go @@ -45,7 +45,10 @@ func ParseUint(s string, base, bitSize int) (uint64, error) { // 2. Handle negative values lesser than MinInt64 if intErr == nil && intValue < 0 { return 0, nil - } else if intErr != nil && intErr.(*strconv.NumError).Err == strconv.ErrRange && intValue < 0 { + // Note errors.Is is not working for strconv errors in Go 1.13, + // see https://github.com/golang/go/issues/36325. + // TODO: remove nolint and switch to errors.Is once we stop supporting Go 1.13. + } else if intErr != nil && intErr.(*strconv.NumError).Err == strconv.ErrRange && intValue < 0 { //nolint:errorlint return 0, nil } diff --git a/libcontainer/intelrdt/intelrdt.go b/libcontainer/intelrdt/intelrdt.go index 0c11615be..04ef6075c 100644 --- a/libcontainer/intelrdt/intelrdt.go +++ b/libcontainer/intelrdt/intelrdt.go @@ -15,6 +15,7 @@ import ( "sync" "github.com/moby/sys/mountinfo" + "github.com/opencontainers/runc/libcontainer/cgroups/fscommon" "github.com/opencontainers/runc/libcontainer/configs" ) @@ -366,24 +367,6 @@ func parseCpuInfoFile(path string) (cpuInfoFlags, error) { return infoFlags, nil } -func parseUint(s string, base, bitSize int) (uint64, error) { - value, err := strconv.ParseUint(s, base, bitSize) - if err != nil { - intValue, intErr := strconv.ParseInt(s, base, bitSize) - // 1. Handle negative values greater than MinInt64 (and) - // 2. Handle negative values lesser than MinInt64 - if intErr == nil && intValue < 0 { - return 0, nil - } else if intErr != nil && intErr.(*strconv.NumError).Err == strconv.ErrRange && intValue < 0 { - return 0, nil - } - - return value, err - } - - return value, nil -} - // Gets a single uint64 value from the specified file. func getIntelRdtParamUint(path, file string) (uint64, error) { fileName := filepath.Join(path, file) @@ -392,7 +375,7 @@ func getIntelRdtParamUint(path, file string) (uint64, error) { return 0, err } - res, err := parseUint(string(bytes.TrimSpace(contents)), 10, 64) + res, err := fscommon.ParseUint(string(bytes.TrimSpace(contents)), 10, 64) if err != nil { return res, fmt.Errorf("unable to parse %q as a uint from file %q", string(contents), fileName) }