Skip to content

Commit

Permalink
filter control chars in torrent names
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Mar 11, 2019
1 parent 88067fc commit a7b161c
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions torrent/stats.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package torrent

import (
"strings"
"sync/atomic"
"time"
"unicode"

"github.com/cenkalti/rain/internal/mse"
"github.com/cenkalti/rain/internal/peer"
Expand Down Expand Up @@ -153,6 +155,7 @@ func (t *torrent) stats() Stats {
} else {
s.Name = t.name
}
s.Name = printableString(s.Name)
if t.bitfield != nil {
s.Pieces.Have = t.bitfield.Count()
s.Pieces.Missing = s.Pieces.Total - s.Pieces.Have
Expand Down Expand Up @@ -185,6 +188,15 @@ func (t *torrent) stats() Stats {
return s
}

func printableString(s string) string {
return strings.Map(func(r rune) rune {
if !unicode.IsPrint(r) {
return unicode.ReplacementChar
}
return r
}, s)
}

func (t *torrent) avaliablePieceCount() uint32 {
if t.piecePicker == nil {
return 0
Expand Down

0 comments on commit a7b161c

Please sign in to comment.