Skip to content

Commit 6b93fa8

Browse files
committed
stream: add bounds checking to RingBuffer.get()
Return `undefined` for out-of-bounds indices instead of wrapping into stale backing array slots via modular arithmetic.
1 parent c6c39d4 commit 6b93fa8

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

lib/internal/streams/iter/ringbuffer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ class RingBuffer {
6565

6666
/**
6767
* Read item at a logical index (0 = head). O(1).
68+
* Returns undefined if index is out of bounds.
6869
* @returns {any}
6970
*/
7071
get(index) {
72+
if (index < 0 || index >= this.#size) return undefined;
7173
return this.#backing[(this.#head + index) % this.#capacity];
7274
}
7375

0 commit comments

Comments
 (0)