Skip to content

Commit

Permalink
bound initial allocation in to_bytes_limited
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Mar 13, 2023
1 parent 9e7a6fe commit 44c5cda
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions actix-http/src/body/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::{BodySize, MessageBody};
pub async fn to_bytes<B: MessageBody>(body: B) -> Result<Bytes, B::Error> {
to_bytes_limited(body, usize::MAX)
.await
.expect("body should never overflow usize::MAX")
.expect("body should never yield more than usize::MAX bytes")
}

/// Error type returned from [`to_bytes_limited`] when body produced exceeds limit.
Expand Down Expand Up @@ -70,12 +70,14 @@ pub async fn to_bytes_limited<B: MessageBody>(
body: B,
limit: usize,
) -> Result<Result<Bytes, B::Error>, BodyLimitExceeded> {
/// Sensible default (32kB) for initial, bounded allocation when collecting body bytes.
const INITIAL_ALLOC_BYTES: usize = 32 * 1024;

let cap = match body.size() {
BodySize::None | BodySize::Sized(0) => return Ok(Ok(Bytes::new())),
BodySize::Sized(size) if size as usize > limit => return Err(BodyLimitExceeded),
BodySize::Sized(size) => size as usize,
// good enough first guess for chunk size
BodySize::Stream => 32_768,
BodySize::Sized(size) => (size as usize).min(INITIAL_ALLOC_BYTES),
BodySize::Stream => INITIAL_ALLOC_BYTES,
};

let mut exceeded_limit = false;
Expand Down

0 comments on commit 44c5cda

Please sign in to comment.