Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coordinator] Ensure does not grow M3Msg buffer if message over max size #2207

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/msg/protocol/proto/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (d *decoder) Decode(m Unmarshaler) error {
if err != nil {
return err
}
d.buffer = growDataBufferIfNeeded(d.buffer, sizeEncodingLength+size, d.bytesPool)
if size > d.maxMessageSize {
return fmt.Errorf("decoded message size %d is larger than maximum supported size %d", size, d.maxMessageSize)
}
d.buffer = growDataBufferIfNeeded(d.buffer, sizeEncodingLength+size, d.bytesPool)
return d.decodeData(d.buffer[sizeEncodingLength:sizeEncodingLength+size], m)
}

Expand Down
11 changes: 10 additions & 1 deletion src/msg/protocol/proto/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,20 @@ func TestDecodeMessageLargerThanMaxSize(t *testing.T) {
require.NoError(t, err)

decodeMsg := msgpb.Message{}
opts := NewOptions().SetMaxMessageSize(4)
opts := NewOptions().SetMaxMessageSize(8)
dec := NewDecoder(bytes.NewReader(enc.Bytes()), opts)

// NB(r): We need to make sure does not grow the buffer
// if over max size, so going to take size of buffer, make
// sure its sizeEncodingLength so we can measure if it increases at all.
require.Equal(t, sizeEncodingLength, cap(dec.(*decoder).buffer))

err = dec.Decode(&decodeMsg)
require.Error(t, err)
require.Contains(t, err.Error(), "larger than maximum supported size")

// Make sure did not grow buffer before returning error.
require.Equal(t, sizeEncodingLength, cap(dec.(*decoder).buffer))
}

func TestEncodeDecodeRoundTrip(t *testing.T) {
Expand Down