From 43418e9f65dc111f1c409a4a5cc10727823df239 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 7 Oct 2013 18:58:33 +1100 Subject: [PATCH] Fix bugs in the new WriteSectionTo hashing implementation --- client.go | 5 +++-- mmap_span.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 1eb15b9d..af67137c 100644 --- a/client.go +++ b/client.go @@ -8,6 +8,7 @@ import ( "crypto/rand" "encoding" "errors" + "fmt" metainfo "github.com/nsf/libtorgo/torrent" "io" "launchpad.net/gommap" @@ -231,12 +232,12 @@ func (t *Torrent) PieceSize(piece int) (size int64) { func (t *Torrent) HashPiece(piece int) (ps pieceSum) { hash := PieceHash.New() - n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, int64(piece)*t.MetaInfo.PieceLength) + n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, t.MetaInfo.PieceLength) if err != nil { panic(err) } if n != t.PieceSize(piece) { - panic("hashed wrong number of bytes") + panic(fmt.Sprintf("hashed wrong number of bytes: expected %d; did %d; piece %d", t.PieceSize(piece), n, piece)) } copyHashSum(ps[:], hash.Sum(nil)) return diff --git a/mmap_span.go b/mmap_span.go index fcc5cd4e..37f97720 100644 --- a/mmap_span.go +++ b/mmap_span.go @@ -61,7 +61,7 @@ func (me MMapSpan) WriteSectionTo(w io.Writer, off, n int64) (written int64, err if err != nil { return true } - return n != 0 + return n == 0 }) return }