Skip to content

Commit

Permalink
Correct the ur-off-by-one that caused all these problems
Browse files Browse the repository at this point in the history
I had an off-by-one error that compounded into multiple horrifying
problems: By inflating the "first" range to the right, we ended up
reading too far (resulting in uninitialized data being possible to
read sometimes). Oops!

That caused several bugs that compensated for the original
problem (and was caused by them in turn), and uuuusually evened
out.

But this is the fact: The "end()" method one past the last index that
can be read (and was wrongly documented to return the index of the
last element that can be read), which means it's exactly a match for
the End field in Range structs. We do not have to add anything to it.
  • Loading branch information
antifuchs committed Mar 13, 2021
1 parent c7c55ef commit 02ac81a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r Ring) Inspect() (first Range, second Range) {
first.Start = r.start()
end1 := r.end()

first.End = end1 + 1
first.End = end1
if end1 <= first.Start {
second.End = end1
first.End = r.capacity()
Expand Down
3 changes: 2 additions & 1 deletion ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type ringBackend interface {
// start returns the index of first element that can be read.
start() uint

// end returns the index of the last element that can be read.
// end returns the index after the last element that can be
// read. It is compatible with go [:] slice expressions.
end() uint

capacity() uint
Expand Down

0 comments on commit 02ac81a

Please sign in to comment.