From 9e8a72ff1d978295556ac3bfbb86b3888e81154a Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 14 Jul 2014 23:12:52 +1000 Subject: [PATCH] Invalidating metadata would cause a crash when metadata pieces kept arriving --- client.go | 2 ++ torrent.go | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 47bc5b6d..956c448e 100644 --- a/client.go +++ b/client.go @@ -494,6 +494,8 @@ func (cl *Client) completedMetadata(t *torrent) { t.InvalidateMetadata() return } + // TODO(anacrolix): If this fails, I think something harsher should be + // done. err = cl.setMetaData(t, info, t.MetaData) if err != nil { log.Printf("error setting metadata: %s", err) diff --git a/torrent.go b/torrent.go index e6844f1c..e9361c86 100644 --- a/torrent.go +++ b/torrent.go @@ -53,9 +53,8 @@ type torrent struct { } func (t *torrent) InvalidateMetadata() { - for i := range t.metadataHave { - t.metadataHave[i] = false - } + t.MetaData = nil + t.metadataHave = nil t.Info = nil } @@ -63,6 +62,10 @@ func (t *torrent) SaveMetadataPiece(index int, data []byte) { if t.haveInfo() { return } + if index >= len(t.metadataHave) { + log.Printf("%s: ignoring metadata piece %d", t, index) + return + } copy(t.MetaData[(1<<14)*index:], data) t.metadataHave[index] = true }