Skip to content

Commit

Permalink
codec: fix LengthDelimitedCodec buffer over-reservation (#4997)
Browse files Browse the repository at this point in the history
  • Loading branch information
yotamofek committed Sep 11, 2022
1 parent 0b1f640 commit 1c82309
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions tokio-util/src/codec/length_delimited.rs
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

0 comments on commit 1c82309

Please sign in to comment.