Skip to content

Commit f2d5624

Browse files
authored
Merge pull request #4405 from amghazanfari/main
replace strings.SplitN with strings.Cut
2 parents 84529ba + faffe1b commit f2d5624

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

exec.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
252252
}
253253
// Override the user, if passed.
254254
if user := context.String("user"); user != "" {
255-
u := strings.SplitN(user, ":", 2)
256-
if len(u) > 1 {
257-
gid, err := strconv.Atoi(u[1])
255+
uids, gids, ok := strings.Cut(user, ":")
256+
if ok {
257+
gid, err := strconv.Atoi(gids)
258258
if err != nil {
259259
return nil, fmt.Errorf("bad gid: %w", err)
260260
}
261261
p.User.GID = uint32(gid)
262262
}
263-
uid, err := strconv.Atoi(u[0])
263+
uid, err := strconv.Atoi(uids)
264264
if err != nil {
265265
return nil, fmt.Errorf("bad uid: %w", err)
266266
}

libcontainer/cgroups/fs/memory.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ func getPageUsageByNUMA(path string) (cgroups.PageUsageByNUMA, error) {
282282
line := scanner.Text()
283283
columns := strings.SplitN(line, " ", maxColumns)
284284
for i, column := range columns {
285-
byNode := strings.SplitN(column, "=", 2)
285+
key, val, ok := strings.Cut(column, "=")
286286
// Some custom kernels have non-standard fields, like
287287
// numa_locality 0 0 0 0 0 0 0 0 0 0
288288
// numa_exectime 0
289-
if len(byNode) < 2 {
289+
if !ok {
290290
if i == 0 {
291291
// Ignore/skip those.
292292
break
@@ -296,7 +296,6 @@ func getPageUsageByNUMA(path string) (cgroups.PageUsageByNUMA, error) {
296296
return stats, malformedLine(path, file, line)
297297
}
298298
}
299-
key, val := byNode[0], byNode[1]
300299
if i == 0 { // First column: key is name, val is total.
301300
field = getNUMAField(&stats, key)
302301
if field == nil { // unknown field (new kernel?)

libcontainer/cgroups/systemd/cpuset.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ func RangeToBits(str string) ([]byte, error) {
2121
if r == "" {
2222
continue
2323
}
24-
ranges := strings.SplitN(r, "-", 2)
25-
if len(ranges) > 1 {
26-
start, err := strconv.ParseUint(ranges[0], 10, 32)
24+
startr, endr, ok := strings.Cut(r, "-")
25+
if ok {
26+
start, err := strconv.ParseUint(startr, 10, 32)
2727
if err != nil {
2828
return nil, err
2929
}
30-
end, err := strconv.ParseUint(ranges[1], 10, 32)
30+
end, err := strconv.ParseUint(endr, 10, 32)
3131
if err != nil {
3232
return nil, err
3333
}
@@ -38,7 +38,7 @@ func RangeToBits(str string) ([]byte, error) {
3838
bits.SetBit(bits, int(i), 1)
3939
}
4040
} else {
41-
val, err := strconv.ParseUint(ranges[0], 10, 32)
41+
val, err := strconv.ParseUint(startr, 10, 32)
4242
if err != nil {
4343
return nil, err
4444
}

libcontainer/init_linux.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,10 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock
259259
// current processes's environment.
260260
func populateProcessEnvironment(env []string) error {
261261
for _, pair := range env {
262-
p := strings.SplitN(pair, "=", 2)
263-
if len(p) < 2 {
262+
name, val, ok := strings.Cut(pair, "=")
263+
if !ok {
264264
return errors.New("invalid environment variable: missing '='")
265265
}
266-
name, val := p[0], p[1]
267266
if name == "" {
268267
return errors.New("invalid environment variable: name cannot be empty")
269268
}

libcontainer/utils/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ func SearchLabels(labels []string, key string) (string, bool) {
101101
func Annotations(labels []string) (bundle string, userAnnotations map[string]string) {
102102
userAnnotations = make(map[string]string)
103103
for _, l := range labels {
104-
parts := strings.SplitN(l, "=", 2)
105-
if len(parts) < 2 {
104+
name, value, ok := strings.Cut(l, "=")
105+
if !ok {
106106
continue
107107
}
108-
if parts[0] == "bundle" {
109-
bundle = parts[1]
108+
if name == "bundle" {
109+
bundle = value
110110
} else {
111-
userAnnotations[parts[0]] = parts[1]
111+
userAnnotations[name] = value
112112
}
113113
}
114114
return

0 commit comments

Comments
 (0)