From 848636f830ec820a0290c4001d220fa93d139ff7 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 22 Aug 2014 01:26:41 +1000 Subject: [PATCH] Fix error in bitfield correction expression and off by one condition when metadata is completed --- connection.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connection.go b/connection.go index 4a7104d9..e79b5d66 100644 --- a/connection.go +++ b/connection.go @@ -100,7 +100,7 @@ func (cn *connection) setNumPieces(num int) error { if len(cn.PeerPieces) == num { } else if len(cn.PeerPieces) < num { cn.PeerPieces = append(cn.PeerPieces, make([]bool, num-len(cn.PeerPieces))...) - } else if len(cn.PeerPieces) < 8*(num+7)/8 { + } else if len(cn.PeerPieces) <= (num+7)/8*8 { for _, have := range cn.PeerPieces[num:] { if have { return errors.New("peer has invalid piece") @@ -108,7 +108,7 @@ func (cn *connection) setNumPieces(num int) error { } cn.PeerPieces = cn.PeerPieces[:num] } else { - return errors.New("peer bitfield is excessively long") + return fmt.Errorf("peer bitfield is excessively long: expected %d, have %d", num, len(cn.PeerPieces)) } if len(cn.PeerPieces) != num { panic("wat") -- 2.48.1