Skip to content

Commit

Permalink
check data length (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin1989 authored and lonng committed Aug 28, 2019
1 parent 15209d9 commit a40ba25
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var (
ErrWrongMessageType = errors.New("wrong message type")
ErrInvalidMessage = errors.New("invalid message")
ErrRouteInfoNotFound = errors.New("route info not found in dictionary")
ErrWrongMessage = errors.New("wrong message")
)

// Message represents a unmarshaled message or a message which to be marshaled
Expand Down Expand Up @@ -190,6 +191,10 @@ func Decode(data []byte) (*Message, error) {
m.ID = id
}

if offset >= len(data) {
return nil, ErrWrongMessage
}

if routable(m.Type) {
if flag&msgRouteCompressMask == 1 {
m.compressed = true
Expand All @@ -204,11 +209,17 @@ func Decode(data []byte) (*Message, error) {
m.compressed = false
rl := data[offset]
offset++
if offset+int(rl) >= len(data) {
return nil, ErrWrongMessage
}
m.Route = string(data[offset:(offset + int(rl))])
offset += int(rl)
}
}

if offset >= len(data) {
return nil, ErrWrongMessage
}
m.Data = data[offset:]
return m, nil
}
Expand Down

0 comments on commit a40ba25

Please sign in to comment.