Skip to content

Commit

Permalink
1445 Replace all min & max constructs with clamp
Browse files Browse the repository at this point in the history
replace .max().min() constructs with .clamp()
  • Loading branch information
chrismiceli authored and bluejekyll committed Feb 17, 2022
1 parent 5aab3c0 commit 13c0793
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions crates/proto/src/https/https_client_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ impl HttpsClientStream {
.map_err(|e| ProtoError::from(format!("bad headers received: {}", e)))?;

// TODO: what is a good max here?
// max(512) says make sure it is at least 512 bytes, and min 4096 says it is at most 4k
// just a little protection from malicious actors.
// clamp(512, 4096) says make sure it is at least 512 bytes, and min 4096 says it is at most 4k
// just a little protection from malicious actors.
let mut response_bytes =
BytesMut::with_capacity(content_length.unwrap_or(512).max(512).min(4096));
BytesMut::with_capacity(content_length.unwrap_or(512).clamp(512, 4096));

while let Some(partial_bytes) = response_stream.body_mut().data().await {
let partial_bytes =
Expand Down
2 changes: 1 addition & 1 deletion crates/proto/src/https/https_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) async fn message_from_post<R>(
where
R: Stream<Item = Result<Bytes, h2::Error>> + 'static + Send + Debug + Unpin,
{
let mut bytes = BytesMut::with_capacity(length.unwrap_or(0).max(512).min(4096));
let mut bytes = BytesMut::with_capacity(length.unwrap_or(0).clamp(512, 4096));

loop {
match request_stream.next().await {
Expand Down
3 changes: 1 addition & 2 deletions crates/resolver/src/dns_lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ impl DnsLru {
let ttl_duration = Duration::from_secs(u64::from(ttl))
// Clamp the TTL so that it's between the cache's configured
// minimum and maximum TTLs for negative responses.
.max(self.negative_min_ttl)
.min(self.negative_max_ttl);
.clamp(self.negative_min_ttl, self.negative_max_ttl);
let valid_until = now + ttl_duration;

{
Expand Down

0 comments on commit 13c0793

Please sign in to comment.