# Bug in RingBuffer.swift

**URL:** <https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063>\
**Category:** Data Structures & Algorithms in Swift\
**Created:** [May 30, 2018, 8:32pm UTC](https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063 "2018-05-30T20:32:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtc](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@jtc](https://forums.kodeco.com/u/jtc)\
**Post date:** [May 30, 2018, 8:32pm UTC](https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063/1 "2018-05-30T20:32:48Z")

</div>

In the CustomStringConvertible extension to RingBuffer, there is a bug in the description var. The line:

var index = readIndex

should be:

var index = readIndex % array.count

Otherwise, when readPointer and writePointer are greater than the size of the array (as they will be after adding/removing multiple items), index will start out being greater than writeIndex % array.count, and the loop is infinite.

Below is some playground code that will crash the current implementation.

```
var queue = QueueRingBuffer<String>(count: 5)
queue.enqueue("Ray")
queue.enqueue("Brian")
queue.enqueue("Eric")
queue
queue.dequeue()
queue
queue.peek

queue.enqueue("1")
queue
queue.enqueue("2")
queue
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.enqueue("3")
queue.enqueue("4")
queue.enqueue("5")
queue
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.enqueue("6")
queue.enqueue("7")
queue.enqueue("8")
queue

```

---

<div class="post-metadata">

**Author:** ![jtc](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@jtc](https://forums.kodeco.com/u/jtc)\
**Post date:** [May 30, 2018, 8:56pm UTC](https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063/2 "2018-05-30T20:56:37Z")

</div>

Also, the implementation of “description” (with my change above) doesn’t work if the ring buffer is full (prints empty brackets). Try this instead–I think it’s a cleaner implementation:

```
public var description: String {
    var result = "["
    for index in readIndex..<writeIndex {
        if let value = array[index % array.count] {
            result += "\(value) "
        }
    }
    result += "]"
    return result
}

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [May 31, 2018, 1:36am UTC](https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063/3 "2018-05-31T01:36:12Z")

</div>

@jomoka Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![jomoka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jomoka/32/109828_2.png) [@jomoka](https://forums.kodeco.com/u/jomoka)\
**Post date:** [June 1, 2018, 4:15am UTC](https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063/4 "2018-06-01T04:15:04Z")

</div>

@jtc thanks a lot, great catch!! We will make this correction in the final release!

---

<div class="post-metadata">

**Author:** ![rayfix](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rayfix/32/108995_2.png) [@rayfix](https://forums.kodeco.com/u/rayfix)\
**Post date:** [June 22, 2018, 10:42pm UTC](https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063/5 "2018-06-22T22:42:16Z")

</div>

This is almost there. It actually will have a problem when the buffer is full (it won’t print anything). Better than crashing though ;]. Anyway, the code that will be in the next. spin coming soon is:

```auto
extension RingBuffer: CustomStringConvertible {
    public var description: String {
        let values = (0..<availableSpaceForReading).map {
            String(describing: array[($0 + readIndex) % array.count]!)
        }
        return "[" + values.joined(separator: ", ") + "]"
    }
}

```

---

<div class="post-metadata">

**Author:** ![jtc](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@jtc](https://forums.kodeco.com/u/jtc)\
**Post date:** [June 24, 2018, 2:56am UTC](https://forums.kodeco.com/t/bug-in-ringbuffer-swift/43063/7 "2018-06-24T02:56:52Z")

</div>

I believe the code I proposed does work when the buffer is full. Could you show me some playground code that breaks it? Maybe I’m missing something here.

readIndex and writeIndex do not wrap back to 0 as they are incremented, so writeIndex will always be bigger than readIndex whenever the buffer is full, and the “for” loop in my code should print the correct number of things…

Thanks!
