-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
replace strings.SplitN with strings.Cut #4405
base: main
Are you sure you want to change the base?
Conversation
exec.go
Outdated
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's common in go to use short variable names when the distance between defining and using them is short; also to avoid type suffixes (like String
here`)
In situations like this, ok
is also a common name (instead of the doesHaveGid
); while doesHaveGid
is somewhat more descriptive, it's likely clear from the code what it's handling; using ok
can reduce cognitive overload, as a reader can recognize the pattern without understanding if doesHaveGid
has additional purpose or (due to the longer name) possibly used further away from its declaration.
e.g., this could look like;
uid, gid, ok := strings.Cut(user, ":")
if ok {
v, err := strconv.Atoi(gid)
if err != nil {
return nil, fmt.Errorf("bad gid: %w", err)
}
p.User.GID = uint32(v)
}
v, err := strconv.Atoi(uid)
if err != nil {
return nil, fmt.Errorf("bad uid: %w", err)
}
p.User.UID = uint32(v)
Somewhat orthogonal to this PR (as it's existing code), but wondering if the strconv.Atoi(gid)
would be better replaced with strconv.ParseUint(gid, 10, 32)
, given that we're ultimately casting to a uint32
; i.e. strconv.Atoi("4294967296")
will result in gid (uint32) 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your comment. I change the variable names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
code changes LGTM, but could you squash the commits?
done |
oh! If you still have time; I notice your DCO sign-off is using your GitHub handle, not your actual name; https://github.com/opencontainers/runc/blob/main/CONTRIBUTING.md#sign-your-work
To be on the safe side (the DCO bot is sometimes picky), you may want to update your Git config ( |
thanks for saying. I changed my user.name as well as my sign |
Signed-off-by: Amir M. Ghazanfari <a.m.ghazanfari76@gmail.com> Signed-off-by: Amir M. Ghazanfari <a.m.ghazanfari76@gmail.com>
there are some cases in the code that this pattern used "strings.SplitN(expression, pattern, 2)"
as we see in #4403 strings.Cut has better performance than strings.SplitN.
i changed some of these cases (for rest of them i wasn't sure that it break the functionality or not)