From: Matt Joiner <anacrolix@gmail.com>
Date: Thu, 8 Feb 2018 04:08:33 +0000 (+1100)
Subject: Fixes for non-pointer Bitmap corrupting state
X-Git-Tag: v1.0.0~194
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=5023dcaa3a757bd79bbceaa638c7fdb462e80082;p=btrtrc.git

Fixes for non-pointer Bitmap corrupting state
---

diff --git a/connection.go b/connection.go
index b68bed7f..09b09a64 100644
--- a/connection.go
+++ b/connection.go
@@ -608,13 +608,13 @@ func (cn *connection) updateRequests() {
 // Emits the indices in the Bitmaps bms in order, never repeating any index.
 // skip is mutated during execution, and its initial values will never be
 // emitted.
-func iterBitmapsDistinct(skip bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
+func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
 	return func(cb iter.Callback) {
 		for _, bm := range bms {
 			if !iter.All(func(i interface{}) bool {
 				skip.Add(i.(int))
 				return cb(i)
-			}, bitmap.Sub(bm, skip).Iter) {
+			}, bitmap.Sub(bm, *skip).Iter) {
 				return
 			}
 		}
@@ -633,7 +633,7 @@ func (cn *connection) unbiasedPieceRequestOrder() iter.Func {
 	// Return an iterator over the different priority classes, minus the skip
 	// pieces.
 	return iter.Chain(
-		iterBitmapsDistinct(skip, now, readahead),
+		iterBitmapsDistinct(&skip, now, readahead),
 		func(cb iter.Callback) {
 			cn.t.pendingPieces.IterTyped(func(piece int) bool {
 				if skip.Contains(piece) {
diff --git a/misc_test.go b/misc_test.go
index 8bb23c35..32e60fa9 100644
--- a/misc_test.go
+++ b/misc_test.go
@@ -25,6 +25,7 @@ func TestIterBitmapsDistinct(t *testing.T) {
 	skip.Add(1)
 	first.Add(1, 0, 3)
 	second.Add(1, 2, 0)
-	assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(skip.Copy(), first, second)))
+	skipCopy := skip.Copy()
+	assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second)))
 	assert.Equal(t, []int{1}, skip.ToSortedSlice())
 }