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

ring index overflow #2627

Closed
tigeriscat opened this issue Nov 22, 2022 · 1 comment · Fixed by #3738
Closed

ring index overflow #2627

tigeriscat opened this issue Nov 22, 2022 · 1 comment · Fixed by #3738

Comments

@tigeriscat
Copy link

Ring index overflow.

Calling Add to exceed MaxInt, the system will panic

Original code
func (r *Ring) Add(v interface{}) {
r.lock.Lock()
defer r.lock.Unlock()

r.elements[r.index%len(r.elements)] = v
r.index++

}

Suggested fixes

func (r *Ring) Add(v interface{}) {
r.lock.Lock()
defer r.lock.Unlock()

r.elements[r.index%len(r.elements)] = v
r.index++

if r.index/len(r.elements) >= 2 {
	r.index = r.index - len(r.elements)
}

}

Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Nov 23, 2023
@kevwan kevwan removed the stale label Nov 23, 2023
@kevwan kevwan linked a pull request Nov 23, 2023 that will close this issue
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 a pull request may close this issue.

2 participants