Commit fba5db20 authored by Brian Tiger Chow's avatar Brian Tiger Chow

Merge pull request #222 from jbenet/fix/peer-string-2014-10-28

fix(peer) String() bounds check
parents 1840a01f 2e0371c6
......@@ -115,8 +115,18 @@ type peer struct {
}
// String prints out the peer.
//
// TODO(brian): ensure correctness at ID generation and
// enforce this by only exposing functions that generate
// IDs safely. Then any peer.ID type found in the
// codebase is known to be correct.
func (p *peer) String() string {
return "[Peer " + p.id.String()[:12] + "]"
pid := p.id.String()
maxRunes := 12
if len(pid) < maxRunes {
maxRunes = len(pid)
}
return "[Peer " + pid[:maxRunes] + "]"
}
// Key returns the ID as a Key (string) for maps.
......
......@@ -46,3 +46,12 @@ func TestNetAddress(t *testing.T) {
t.Error("NetAddress lookup failed", udp, udp2)
}
}
func TestStringMethodWithSmallId(t *testing.T) {
p := WithID([]byte(string(0)))
p1, ok := p.(*peer)
if !ok {
t.Fatal("WithID doesn't return a peer")
}
p1.String()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment