Keep length as uint for overflow check

This commit is contained in:
Justin Nuß 2015-10-22 12:21:04 +02:00
parent cbdc43cf9a
commit da435e3a08
1 changed files with 3 additions and 3 deletions

View File

@ -160,7 +160,7 @@ func (p *parser) recvMsg() (pf payloadFormat, msg []byte, err error) {
// generates the message header of 0 message length.
func encode(c Codec, msg interface{}, pf payloadFormat) ([]byte, error) {
var b []byte
var length uint32
var length uint
if msg != nil {
var err error
// TODO(zhaoq): optimize to reduce memory alloc and copying.
@ -168,7 +168,7 @@ func encode(c Codec, msg interface{}, pf payloadFormat) ([]byte, error) {
if err != nil {
return nil, err
}
length = uint32(len(b))
length = uint(len(b))
}
if length > math.MaxUint32 {
return nil, Errorf(codes.InvalidArgument, "grpc: message too large (%d bytes)", length)
@ -184,7 +184,7 @@ func encode(c Codec, msg interface{}, pf payloadFormat) ([]byte, error) {
// Write payload format
buf[0] = byte(pf)
// Write length of b into buf
binary.BigEndian.PutUint32(buf[1:], length)
binary.BigEndian.PutUint32(buf[1:], uint32(length))
// Copy encoded msg to buf
copy(buf[5:], b)