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

RingBuffer with a size of 1 should not be allowed due to an insert bug, so increment a size of one, by one. #218

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

Conversation

hedisam
Copy link

@hedisam hedisam commented Apr 23, 2021

RingBuffer with a size of 1 will have a mask of 0 which uncovers a bug in the algorithm since the mask value must only contain ones (left padded with zeros) if it's represented in its binary format (e.g. '0111', '0011 1111', etc.). So we should not allow having RingBuffer queues with a size of 1. We can simply fix this by incrementing the size inside the RingBuffer's init method.

With a full RingBuffer of size 1, insert operations replace the existing item in the queue and then puts the RingBuffer into an invalid state.

… 1 due to occuring replacements having a full queue with a size of one
@aviary-wf
Copy link

Security Insights

No security relevant content was detected by automated scans.

Action Items

  • Review PR for security impact; comment "security review required" if needed or unsure
  • Verify aviary.yaml coverage of security relevant code

Questions or Comments? Reach out on Slack: #support-infosec.

@hedisam
Copy link
Author

hedisam commented Apr 23, 2021

You can see the bug in action by running this unit test:

func TestRingInsertSizeOne(t *testing.T) {
	rb := NewRingBuffer(1)
	assert.Equal(t, uint64(1), rb.Cap())

	err := rb.Put("Hello")
	if !assert.Nil(t, err) {
		return
	}

	ok, err := rb.Offer("Hello, Again.")
	if !assert.Nil(t, err) {
		return
	}
	assert.False(t, ok)

        // rb.Get() blocks the routine since our RingBuffer's state becomes invalid 
	result, err := rb.Poll(time.Millisecond * 100)
	if !assert.Nil(t, err) {
		return
	}
	if !assert.NotNil(t, result) {
		return
	}
	assert.Equal(t, "Hello", result)
}

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.

2 participants