Skip to content

Commit

Permalink
replace strings.SplitN with strings.Cut
Browse files Browse the repository at this point in the history
Signed-off-by:  Amir M. Ghazanfari <[email protected]>
Signed-off-by: Amir M. Ghazanfari <[email protected]>
  • Loading branch information
amghazanfari committed Sep 20, 2024
1 parent 1590491 commit ee73a09
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
8 changes: 4 additions & 4 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
uids, gids, ok := strings.Cut(user, ":")
if ok {
gid, err := strconv.Atoi(gids)
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(uids)
if err != nil {
return nil, fmt.Errorf("bad uid: %w", err)
}
Expand Down
13 changes: 6 additions & 7 deletions libcontainer/cgroups/fs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ok := 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 !ok {
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?)
Expand Down
10 changes: 5 additions & 5 deletions libcontainer/cgroups/systemd/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
startr, endr, ok := strings.Cut(r, "-")
if ok {
start, err := strconv.ParseUint(startr, 10, 32)
if err != nil {
return nil, err
}
end, err := strconv.ParseUint(ranges[1], 10, 32)
end, err := strconv.ParseUint(endr, 10, 32)
if err != nil {
return nil, err
}
Expand All @@ -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(startr, 10, 32)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ok := strings.Cut(pair, "=")
if !ok {
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")
}
Expand Down
10 changes: 5 additions & 5 deletions libcontainer/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ok := strings.Cut(l, "=")
if !ok {
continue
}
if parts[0] == "bundle" {
bundle = parts[1]
if name == "bundle" {
bundle = value
} else {
userAnnotations[parts[0]] = parts[1]
userAnnotations[name] = value
}
}
return
Expand Down

0 comments on commit ee73a09

Please sign in to comment.