[Repo Assist] Fix CircularBuffer.Enqueue(array) - head pointer advances by count, not 1#233
Conversation
When enqueueing an array of count elements, the head pointer was only advanced by 1 instead of count, causing subsequent array enqueues to overwrite incorrect positions in the buffer. The fix separates the start position calculation from the head update: - startPos = (head + 1) % bufferSize (where writing begins) - head = (head + count) % bufferSize (where head ends after writing) Also enables the three ptest tests that were previously pending due to this bug, confirming they now pass. Closes #125 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@gdziadkiewicz Over to you :) |
|
@gdziadkiewicz Repo Assist creates all PRs in draft mode. There is a CI trigger in place so CI will run automatically. If it's green you can usually take them out of draft mode and assess. |
|
Hi @dsyme , I somehow missed your comment here, sorry! I'm going through the PRs, reviewing and merging (starting with the stuff that is less risky or I feel more confident about). Where can I find more about repo-assist? Why did it got disabled in the repo? Can I reenable it? I'm happy with the experience, especially when mixing repos assists with Copilot review + Copliot "do that" and with human review. |
…array-enqueue-806b1b4611ff3be2
There was a problem hiding this comment.
Pull request overview
Fixes incorrect head advancement when enqueueing multiple elements into CircularBuffer, ensuring subsequent enqueues/dequeues operate on the correct positions and previously-pending tests can be enabled.
Changes:
- Correct
CircularBuffer.Enqueue(array, offset, count)to compute the write start position separately and advanceheadbycount. - Enable three previously-pending CircularBuffer array enqueue tests now that they pass.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/FSharpx.Collections/CircularBuffer.fs |
Fixes head update logic for array enqueues to advance by count rather than 1. |
tests/FSharpx.Collections.Tests/CircularBufferTests.fs |
Promotes three ptest cases to active test coverage for array enqueue scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let startPos = (head + 1) % bufferSize | ||
|
|
||
| for x, y in nextBuffer head count do | ||
| for x, y in nextBuffer startPos count do | ||
| Array.blit value offset buffer x y | ||
| offset <- offset + y |
There was a problem hiding this comment.
Enqueue(value: _[], offset, count) doesn't validate offset/count against value.Length (and non-negative). If Array.blit throws (e.g., offset + count > value.Length), the buffer may already be partially overwritten (especially when length = bufferSize), leaving the collection in a corrupted state. Consider validating inputs upfront so the operation either completes fully or fails without mutating the underlying buffer.
There was a problem hiding this comment.
@copilot Add validation of count and offset and add tests for it
There was a problem hiding this comment.
Added in commit 028c9f9. Enqueue(value, offset, count) now validates upfront (before any mutation):
valueis not nulloffsetis non-negativecountis non-negativeoffset + countdoes not exceedvalue.Length
Three new tests cover each of the invalid-argument cases.
Agent-Logs-Url: https://github.com/fsprojects/FSharpx.Collections/sessions/650cdd45-dd1e-4c67-8bc3-2a4dcbc5e426 Co-authored-by: gdziadkiewicz <8547855+gdziadkiewicz@users.noreply.github.com>
…array-enqueue-806b1b4611ff3be2
🤖 This PR was created by Repo Assist, an automated AI assistant.
Closes #125
Root Cause
In
CircularBuffer.Enqueue(value: _[], offset, count), theheadpointer was unconditionally advanced by 1, regardless of how many elements were being enqueued:This meant that after enqueueing an array of N elements,
headpointed to the start of the written region, not the end. Subsequent enqueue operations then computed the wrong start position, causing data to be written to incorrect buffer locations and thetailpointer to be miscalculated.Fix
Separate the start-position calculation from the head update, and add upfront input validation:
For
count = 1(single-element enqueue, which delegates here) the behaviour is identical to before.Input validation is now performed before any buffer mutation:
valuemust not be nulloffsetmust be non-negativecountmust be non-negativeoffset + countmust not exceedvalue.LengthChanges
src/FSharpx.Collections/CircularBuffer.fs— fix the head advancement logic; add upfrontoffset/countvalidation to prevent partial buffer corruption on bad inputtests/FSharpx.Collections.Tests/CircularBufferTests.fs— promote the threeptest(pending/skipped) tests totestsince they now pass; add three new tests covering invalidoffset/countargumentsTest Status
✅
FSharpx.Collections.Tests: 839 passed, 2 ignored, 0 failed (includes the three newly-enabled CircularBuffer array tests and three new validation tests)FSharpx.Collections.Experimental.Tests: killed with exit code 137 (OOM) — this is a pre-existing infrastructure issue unrelated to this change (see issues #115 and #116).🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.