From ac50179bb667b49f88f93d48b3c44393eb3b5fe4 Mon Sep 17 00:00:00 2001 From: amghazanfari Date: Thu, 19 Sep 2024 10:51:37 +0330 Subject: [PATCH] replace strings.SplitN with strings.Cut Signed-off-by: amghazanfari --- exec.go | 8 ++++---- libcontainer/cgroups/fs/memory.go | 13 ++++++------- libcontainer/cgroups/systemd/cpuset.go | 10 +++++----- libcontainer/init_linux.go | 5 ++--- libcontainer/integration/update_test.go | 2 ++ libcontainer/utils/utils.go | 10 +++++----- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/exec.go b/exec.go index ad8a369a5dd..a03c0b98008 100644 --- a/exec.go +++ b/exec.go @@ -247,15 +247,15 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) { } // Override the user, if passed. if user := context.String("user"); user != "" { - u := strings.SplitN(user, ":", 2) - if len(u) > 1 { - gid, err := strconv.Atoi(u[1]) + uidString, gidString, doesHaveGid := strings.Cut(user, ":") + if doesHaveGid { + gid, err := strconv.Atoi(gidString) if err != nil { return nil, fmt.Errorf("bad gid: %w", err) } p.User.GID = uint32(gid) } - uid, err := strconv.Atoi(u[0]) + uid, err := strconv.Atoi(uidString) if err != nil { return nil, fmt.Errorf("bad uid: %w", err) } diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index 783566d68f0..f867b9e2f8f 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -282,21 +282,20 @@ func getPageUsageByNUMA(path string) (cgroups.PageUsageByNUMA, error) { line := scanner.Text() columns := strings.SplitN(line, " ", maxColumns) for i, column := range columns { - byNode := strings.SplitN(column, "=", 2) + key, val, isValidNumaStats := strings.Cut(column, "=") // Some custom kernels have non-standard fields, like // numa_locality 0 0 0 0 0 0 0 0 0 0 // numa_exectime 0 - if len(byNode) < 2 { - if i == 0 { - // Ignore/skip those. - break - } else { + if !isValidNumaStats { + if i != 0 { // The first column was already validated, // so be strict to the rest. return stats, malformedLine(path, file, line) } + + // Ignore/skip those. + break } - key, val := byNode[0], byNode[1] if i == 0 { // First column: key is name, val is total. field = getNUMAField(&stats, key) if field == nil { // unknown field (new kernel?) diff --git a/libcontainer/cgroups/systemd/cpuset.go b/libcontainer/cgroups/systemd/cpuset.go index dd474cf1b16..a3dfea71c67 100644 --- a/libcontainer/cgroups/systemd/cpuset.go +++ b/libcontainer/cgroups/systemd/cpuset.go @@ -21,13 +21,13 @@ func RangeToBits(str string) ([]byte, error) { if r == "" { continue } - ranges := strings.SplitN(r, "-", 2) - if len(ranges) > 1 { - start, err := strconv.ParseUint(ranges[0], 10, 32) + startRange, endRange, doesHaveEnd := strings.Cut(r, "-") + if doesHaveEnd { + start, err := strconv.ParseUint(startRange, 10, 32) if err != nil { return nil, err } - end, err := strconv.ParseUint(ranges[1], 10, 32) + end, err := strconv.ParseUint(endRange, 10, 32) if err != nil { return nil, err } @@ -38,7 +38,7 @@ func RangeToBits(str string) ([]byte, error) { bits.SetBit(bits, int(i), 1) } } else { - val, err := strconv.ParseUint(ranges[0], 10, 32) + val, err := strconv.ParseUint(startRange, 10, 32) if err != nil { return nil, err } diff --git a/libcontainer/init_linux.go b/libcontainer/init_linux.go index 90f40cad257..70c731be73c 100644 --- a/libcontainer/init_linux.go +++ b/libcontainer/init_linux.go @@ -259,11 +259,10 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock // current processes's environment. func populateProcessEnvironment(env []string) error { for _, pair := range env { - p := strings.SplitN(pair, "=", 2) - if len(p) < 2 { + name, val, isValidEnv := strings.Cut(pair, "=") + if !isValidEnv { return errors.New("invalid environment variable: missing '='") } - name, val := p[0], p[1] if name == "" { return errors.New("invalid environment variable: name cannot be empty") } diff --git a/libcontainer/integration/update_test.go b/libcontainer/integration/update_test.go index 5678b6f6364..ce1c9c3acb3 100644 --- a/libcontainer/integration/update_test.go +++ b/libcontainer/integration/update_test.go @@ -2,6 +2,7 @@ package integration import ( "bytes" + "fmt" "os" "strings" "testing" @@ -23,6 +24,7 @@ func testUpdateDevices(t *testing.T, systemd bool) { // Execute a first process in the container stdinR, stdinW, err := os.Pipe() ok(t, err) + fmt.Println(standardEnvironment) process := &libcontainer.Process{ Cwd: "/", Args: []string{"cat"}, diff --git a/libcontainer/utils/utils.go b/libcontainer/utils/utils.go index 793783929fb..cf006ca7dc1 100644 --- a/libcontainer/utils/utils.go +++ b/libcontainer/utils/utils.go @@ -101,14 +101,14 @@ func SearchLabels(labels []string, key string) (string, bool) { func Annotations(labels []string) (bundle string, userAnnotations map[string]string) { userAnnotations = make(map[string]string) for _, l := range labels { - parts := strings.SplitN(l, "=", 2) - if len(parts) < 2 { + name, value, isValidAnnotation := strings.Cut(l, "=") + if !isValidAnnotation { continue } - if parts[0] == "bundle" { - bundle = parts[1] + if name == "bundle" { + bundle = value } else { - userAnnotations[parts[0]] = parts[1] + userAnnotations[name] = value } } return