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

View File

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