Skip to content

Commit

Permalink
fix: correctly reserve and zero buf for blocking Body read (#2533)
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini authored Jan 27, 2025
1 parent 28d25bd commit b65a03c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ __rustls-ring = ["hyper-rustls?/ring", "tokio-rustls?/ring", "rustls?/ring", "qu
base64 = "0.22"
http = "1"
url = "2.4"
bytes = "1.0"
bytes = "1.2"
serde = "1.0"
serde_urlencoded = "0.7.1"
tower-service = "0.3"
Expand Down
17 changes: 10 additions & 7 deletions src/blocking/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use std::future::Future;
#[cfg(feature = "multipart")]
use std::io::Cursor;
use std::io::{self, Read};
use std::mem;
use std::mem::{self, MaybeUninit};
use std::ptr;

use bytes::buf::UninitSlice;
use bytes::Bytes;
use futures_channel::mpsc;

Expand Down Expand Up @@ -280,7 +279,8 @@ async fn send_future(sender: Sender) -> Result<(), crate::Error> {
let con_len = sender.body.1;
let cap = cmp::min(sender.body.1.unwrap_or(8192), 8192);
let mut written = 0;
let mut buf = BytesMut::with_capacity(cap as usize);
let mut buf = BytesMut::zeroed(cap as usize);
buf.clear();
let mut body = sender.body.0;
// Put in an option so that it can be consumed on error to call abort()
let mut tx = Some(sender.tx);
Expand All @@ -305,16 +305,19 @@ async fn send_future(sender: Sender) -> Result<(), crate::Error> {
// This behaviour is questionable, but it exists and the
// fact is that there is actually no remaining data to read.
if buf.is_empty() {
if buf.remaining_mut() == 0 {
if buf.capacity() == buf.len() {
buf.reserve(8192);
// zero out the reserved memory
let uninit = buf.chunk_mut();
let uninit = buf.spare_capacity_mut();
let uninit_len = uninit.len();
unsafe {
ptr::write_bytes(uninit.as_mut_ptr(), 0, uninit.len());
ptr::write_bytes(uninit.as_mut_ptr().cast::<u8>(), 0, uninit_len);
}
}

let bytes = unsafe { mem::transmute::<&mut UninitSlice, &mut [u8]>(buf.chunk_mut()) };
let bytes = unsafe {
mem::transmute::<&mut [MaybeUninit<u8>], &mut [u8]>(buf.spare_capacity_mut())
};
match body.read(bytes) {
Ok(0) => {
// The buffer was empty and nothing's left to
Expand Down

0 comments on commit b65a03c

Please sign in to comment.