-
Notifications
You must be signed in to change notification settings - Fork 77
[Repo Assist] fix: CircularBuffer.GetEnumerator infinite-recursion and destructive-enumeration #257
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -320,4 +320,54 @@ module CircularBufferTests = | |||||||||
| //|> Async.Start | ||||||||||
| //// [/snippet] | ||||||||||
| //printfn "CircularStream.AsyncWrite(array) tests passed in %d ms" stopwatch.ElapsedMilliseconds | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| test "Enqueue(array, offset) 2-arg overload enqueues from offset to end" { | ||||||||||
| let circularBuffer = CircularBuffer<int> 5 | ||||||||||
| circularBuffer.Enqueue([| 10; 20; 30; 40; 50 |], 2) | ||||||||||
| Expect.equal "count" 3 <| circularBuffer.Count | ||||||||||
| Expect.equal "dequeued" [| 30; 40; 50 |] <| circularBuffer.Dequeue 3 | ||||||||||
| } | ||||||||||
|
|
||||||||||
| test "Seq.toList is non-destructive: buffer is unchanged after enumeration" { | ||||||||||
| let circularBuffer = CircularBuffer<int> 5 | ||||||||||
| circularBuffer.Enqueue(1) | ||||||||||
| circularBuffer.Enqueue(2) | ||||||||||
| circularBuffer.Enqueue(3) | ||||||||||
| let asList = Seq.toList circularBuffer | ||||||||||
| Expect.equal "seq contents" [ 1; 2; 3 ] asList | ||||||||||
| Expect.equal "count unchanged" 3 <| circularBuffer.Count | ||||||||||
| } | ||||||||||
|
|
||||||||||
| test "for-in loop enumerates elements in FIFO order" { | ||||||||||
| let circularBuffer = CircularBuffer<int> 5 | ||||||||||
| circularBuffer.Enqueue [| 10; 20; 30 |] | ||||||||||
| let mutable result = [] | ||||||||||
|
|
||||||||||
| for x in circularBuffer do | ||||||||||
| result <- result @ [ x ] | ||||||||||
|
|
||||||||||
|
Comment on lines
+347
to
+348
|
||||||||||
| result <- result @ [ x ] | |
| result <- x :: result | |
| let result = List.rev result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetEnumeratorbuilds a seq that reads the mutabletailfield on each iteration (buffer.[(tail + i) % bufferSize]). If the buffer is modified during enumeration, this can shift the effective start index mid-enumeration and produce duplicated/skipped elements. Consider snapshottingtail(andlength) into localletbindings before constructing the sequence so the enumerator is stable for the duration of enumeration.