Fix crash when KRPC messages don't contain a valid "t" field

This commit is contained in:
Matt Joiner 2014-07-16 17:10:17 +10:00
parent b30f3ba73e
commit e2fc96cc5a
1 changed files with 11 additions and 2 deletions

View File

@ -64,6 +64,15 @@ func (m Msg) String() string {
return fmt.Sprintf("%#v", m) return fmt.Sprintf("%#v", m)
} }
func (m Msg) T() (t string) {
tif, ok := m["t"]
if !ok {
return
}
t, _ = tif.(string)
return
}
type transaction struct { type transaction struct {
remoteAddr net.Addr remoteAddr net.Addr
t string t string
@ -128,7 +137,7 @@ func (s *Server) Serve() error {
if err != nil { if err != nil {
return err return err
} }
var d map[string]interface{} var d Msg
err = bencode.Unmarshal(b[:n], &d) err = bencode.Unmarshal(b[:n], &d)
if err != nil { if err != nil {
log.Printf("%s: received bad krpc message: %s: %q", s, err, b[:n]) log.Printf("%s: received bad krpc message: %s: %q", s, err, b[:n])
@ -140,7 +149,7 @@ func (s *Server) Serve() error {
s.mu.Unlock() s.mu.Unlock()
continue continue
} }
t := s.findResponseTransaction(d["t"].(string), addr) t := s.findResponseTransaction(d.T(), addr)
if t == nil { if t == nil {
//log.Printf("unexpected message: %#v", d) //log.Printf("unexpected message: %#v", d)
s.mu.Unlock() s.mu.Unlock()