Add error return value to metainfo.MetaInfo.UnmarshalInfo

Too many problems caused by bad info bytes in downstream projects.
This commit is contained in:
Matt Joiner 2016-09-20 12:25:40 +10:00
parent 9a4fbb01f0
commit b304baad1b
4 changed files with 8 additions and 9 deletions

View File

@ -74,7 +74,9 @@ func TestMagnetize(t *testing.T) {
mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent")
require.NoError(t, err)
m := mi.Magnet(mi.UnmarshalInfo().Name, mi.HashInfoBytes())
info, err := mi.UnmarshalInfo()
require.NoError(t, err)
m := mi.Magnet(info.Name, mi.HashInfoBytes())
assert.EqualValues(t, "bootstrap.dat", m.DisplayName)

View File

@ -1,7 +1,6 @@
package metainfo
import (
"fmt"
"io"
"os"
"time"
@ -49,11 +48,8 @@ func LoadFromFile(filename string) (*MetaInfo, error) {
return Load(f)
}
func (mi MetaInfo) UnmarshalInfo() (info Info) {
err := bencode.Unmarshal(mi.InfoBytes, &info)
if err != nil {
panic(fmt.Sprintf("bad info bytes: %s", err))
}
func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
err = bencode.Unmarshal(mi.InfoBytes, &info)
return
}

View File

@ -18,8 +18,9 @@ import (
func testFile(t *testing.T, filename string) {
mi, err := LoadFromFile(filename)
require.NoError(t, err)
info, err := mi.UnmarshalInfo()
require.NoError(t, err)
info := mi.UnmarshalInfo()
if len(info.Files) == 1 {
t.Logf("Single file: %s (length: %d)\n", info.Name, info.Files[0].Length)
} else {

View File

@ -34,7 +34,7 @@ func TorrentSpecFromMagnetURI(uri string) (spec *TorrentSpec, err error) {
}
func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec) {
info := mi.UnmarshalInfo()
info, _ := mi.UnmarshalInfo()
spec = &TorrentSpec{
Trackers: mi.AnnounceList,
InfoBytes: mi.InfoBytes,