From 1c5bd1855a36853b83fe745a018c23cd2609e787 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 7 Mar 2015 17:09:18 +1100 Subject: [PATCH] Improvements to P2P blocklist scanning errors --- client.go | 4 +++- iplist/iplist.go | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 9a427faf..eddfcff8 100644 --- a/client.go +++ b/client.go @@ -380,12 +380,14 @@ func (cl *Client) setEnvBlocklist() (err error) { var ranges []iplist.Range uniqStrs := make(map[string]string) scanner := bufio.NewScanner(f) + lineNum := 1 for scanner.Scan() { r, ok, lineErr := iplist.ParseBlocklistP2PLine(scanner.Bytes()) if lineErr != nil { - err = fmt.Errorf("error reading torrent blocklist line: %s", lineErr) + err = fmt.Errorf("error reading torrent blocklist line %d: %s", lineNum, lineErr) return } + lineNum++ if !ok { continue } diff --git a/iplist/iplist.go b/iplist/iplist.go index 756c0263..2145b297 100644 --- a/iplist/iplist.go +++ b/iplist/iplist.go @@ -2,6 +2,7 @@ package iplist import ( "bytes" + "errors" "fmt" "net" "sort" @@ -67,7 +68,16 @@ func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) { return } colon := bytes.IndexByte(l, ':') - hyphen := bytes.IndexByte(l[colon+1:], '-') + colon + 1 + if colon == -1 { + err = errors.New("missing colon") + return + } + hyphen := bytes.IndexByte(l[colon+1:], '-') + if hyphen == -1 { + err = errors.New("missing hyphen") + return + } + hyphen += colon + 1 r.Description = string(l[:colon]) r.First = net.ParseIP(string(l[colon+1 : hyphen])) r.Last = net.ParseIP(string(l[hyphen+1:]))