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

Fix LengthDelimitedCodec buffer over-reservation. #4997

Merged
merged 1 commit into from
Sep 11, 2022
Merged
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
10 changes: 3 additions & 7 deletions tokio-util/src/codec/length_delimited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,11 @@ impl LengthDelimitedCodec {
}
};

let num_skip = self.builder.get_num_skip();

if num_skip > 0 {
src.advance(num_skip);
}
src.advance(self.builder.get_num_skip());

// Ensure that the buffer has enough space to read the incoming
// payload
src.reserve(n);
src.reserve(n.saturating_sub(src.len()));

Ok(Some(n))
}
Expand Down Expand Up @@ -568,7 +564,7 @@ impl Decoder for LengthDelimitedCodec {
self.state = DecodeState::Head;

// Make sure the buffer has enough space to read the next head
src.reserve(self.builder.num_head_bytes());
src.reserve(self.builder.num_head_bytes().saturating_sub(src.len()));

Ok(Some(data))
}
Expand Down