Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

amghazanfari
Copy link

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)

exec.go Outdated
Comment on lines 250 to 261
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)
}
Copy link
Member

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.

Copy link
Author

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.

libcontainer/init_linux.go Outdated Show resolved Hide resolved
libcontainer/utils/utils.go Outdated Show resolved Hide resolved
Copy link
Member

@thaJeztah thaJeztah left a 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?

@amghazanfari
Copy link
Author

thanks!

code changes LGTM, but could you squash the commits?

done

@thaJeztah
Copy link
Member

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

using your real name (sorry, no pseudonyms or anonymous contributions.)

To be on the safe side (the DCO bot is sometimes picky), you may want to update your Git config (user.name) as well accordingly.

@amghazanfari
Copy link
Author

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

using your real name (sorry, no pseudonyms or anonymous contributions.)

To be on the safe side (the DCO bot is sometimes picky), you may want to update your Git config (user.name) as well accordingly.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants