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: prevent ring index overflow #3738

Merged
merged 2 commits into from
Nov 23, 2023
Merged

fix: prevent ring index overflow #3738

merged 2 commits into from
Nov 23, 2023

Conversation

POABOB
Copy link
Contributor

@POABOB POABOB commented Nov 23, 2023

I added these lines to prevent the ring index from overflowing.

// Add adds v into r.
func (r *Ring) Add(v any) {
	r.lock.Lock()
	defer r.lock.Unlock()

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

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

The issue is from #2627.

Copy link

codecov bot commented Nov 23, 2023

Codecov Report

Merging #3738 (a960aa5) into master (5e8e21b) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

Additional details and impacted files
Files Coverage Δ
core/collection/ring.go 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@POABOB POABOB changed the title fix: prevent ring index overflow (#2627) fix: prevent ring index overflow Nov 23, 2023
@kevwan kevwan linked an issue Nov 23, 2023 that may be closed by this pull request
@kevwan kevwan added this pull request to the merge queue Nov 23, 2023
Merged via the queue into zeromicro:master with commit c220b5d Nov 23, 2023
6 checks passed
WqyJh pushed a commit to WqyJh/go-zero that referenced this pull request Dec 21, 2023
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.

ring index overflow
2 participants