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

fix negative slice index error in keymutex #84

Merged
merged 1 commit into from
Feb 21, 2019

Conversation

danielqsj
Copy link
Contributor

xref: kubernetes/kubernetes#73858

The keymutex's slice index could be negative which should not happened.
This PR aims to make the index always positive.

/assign @thockin @dims

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 19, 2019
@danielqsj
Copy link
Contributor Author

weird, why cla check failed ?

@dims
Copy link
Member

dims commented Feb 19, 2019

@danielqsj can you please sign the CLA? See https://github.com/kubernetes/community/blob/master/CLA.md for instructions and trouble shooting steps

@danielqsj
Copy link
Contributor Author

@dims I signed the CLA for CNCF a long time ago. And also checked again.
I'm also a member of kubernetes. So is there some issues about the check here because my PRs in k/k can pass CLA check.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Feb 19, 2019
@danielqsj
Copy link
Contributor Author

@dims oops, thank you so much.

@dims
Copy link
Member

dims commented Feb 19, 2019

@danielqsj is there some kind of test we could add?

Copy link
Member

@apelisse apelisse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielqsj is there some kind of test we could add?

Maybe a good use-case for a fuzzer?

return nil
}

func (km *hashedKeyMutex) hash(id string) int {
func (km *hashedKeyMutex) hash(id string) uint {
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 fine to cast from uint32 to uint, but I'm not sure why we're doing that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @apelisse
The issue mentions that on arm machine, the output of int(h.Sum32()) maybe negative, and cause problems in LockKey and UnlockKey due to this negative number will be used as the slice index.

But I'm not sure, whether convert to uint is safe here.

Otherwise, math.Abs could be another option for this case ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry I made myself unclear. I meant, why don't you just return an uncasted uint32?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha, that's right. I miss the return type of h.Sum32(). Thanks.
Already updated, PTAL.

}

// Releases the lock associated with the specified ID.
func (km *hashedKeyMutex) UnlockKey(id string) error {
km.mutexes[km.hash(id)%len(km.mutexes)].Unlock()
km.mutexes[km.hash(id)%uint(len(km.mutexes))].Unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think it's fine to cast the return of len() (int) into uint since len is always returning a zero or positive value.

}

// Releases the lock associated with the specified ID.
func (km *hashedKeyMutex) UnlockKey(id string) error {
km.mutexes[km.hash(id)%len(km.mutexes)].Unlock()
km.mutexes[km.hash(id)%uint32(len(km.mutexes))].Unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yes, this is fine.

@apelisse
Copy link
Member

/lgtm

@apelisse
Copy link
Member

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: apelisse, danielqsj

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 21, 2019
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 21, 2019
@k8s-ci-robot k8s-ci-robot merged commit c2654d5 into kubernetes:master Feb 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants