Skip to content

Commit

Permalink
fix(http1) bump itoa version, don't use mem::uninit
Browse files Browse the repository at this point in the history
In newer versions of rust, mem::uninitialized and mem::zeroed will panic
when they are used for types that don't allow it in more cases.

This change allows the tests to run successfully even with the strictest
form of the checks.
  • Loading branch information
5225225 committed Jul 12, 2022
1 parent d7495a7 commit 131bf52
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ http-body = "0.3.1"
httpdate = "0.3"
httparse = "1.0"
h2 = "0.2.2"
itoa = "0.4.1"
itoa = "1"
tracing = { version = "0.1", default-features = false, features = ["log", "std"] }
pin-project = "1.0"
tower-service = "0.3"
Expand All @@ -56,7 +56,7 @@ tower-util = "0.3"
url = "1.0"

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dev-dependencies]
pnet = "0.25.0"
pnet = "0.29.0"

[features]
default = [
Expand Down
17 changes: 6 additions & 11 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,9 @@ impl Http1Transaction for Server {
let len;
let headers_len;

// Unsafe: both headers_indices and headers are using uninitialized memory,
// but we *never* read any of it until after httparse has assigned
// values into it. By not zeroing out the stack memory, this saves
// a good ~5% on pipeline benchmarks.
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::uninitialized() };
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::zeroed() };
{
let mut headers: [httparse::Header<'_>; MAX_HEADERS] = unsafe { mem::uninitialized() };
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
trace!(
"Request.parse([Header; {}], [u8; {}])",
headers.len(),
Expand Down Expand Up @@ -562,7 +558,8 @@ impl Http1Transaction for Server {
Encoder::length(0)
} else {
extend(dst, b"content-length: ");
let _ = ::itoa::write(&mut dst, len);
let mut buf = itoa::Buffer::new();
extend(dst, buf.format(len).as_bytes());
extend(dst, b"\r\n");
Encoder::length(len)
}
Expand Down Expand Up @@ -650,11 +647,9 @@ impl Http1Transaction for Client {

// Loop to skip information status code headers (100 Continue, etc).
loop {
// Unsafe: see comment in Server Http1Transaction, above.
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::uninitialized() };
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::zeroed() };
let (len, status, version, headers_len) = {
let mut headers: [httparse::Header<'_>; MAX_HEADERS] =
unsafe { mem::uninitialized() };
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
trace!(
"Response.parse([Header; {}], [u8; {}])",
headers.len(),
Expand Down

0 comments on commit 131bf52

Please sign in to comment.