Skip to content

Commit caf5a1e

Browse files
committed
fix(http1) fix invalid use of mem::uninit
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.
1 parent d7495a7 commit caf5a1e

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ http-body = "0.3.1"
2929
httpdate = "0.3"
3030
httparse = "1.0"
3131
h2 = "0.2.2"
32-
itoa = "0.4.1"
32+
itoa = "1"
3333
tracing = { version = "0.1", default-features = false, features = ["log", "std"] }
3434
pin-project = "1.0"
3535
tower-service = "0.3"
@@ -56,7 +56,7 @@ tower-util = "0.3"
5656
url = "1.0"
5757

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

6161
[features]
6262
default = [

src/proto/h1/role.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,9 @@ impl Http1Transaction for Server {
111111
let len;
112112
let headers_len;
113113

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

651648
// Loop to skip information status code headers (100 Continue, etc).
652649
loop {
653-
// Unsafe: see comment in Server Http1Transaction, above.
654-
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::uninitialized() };
650+
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::zeroed() };
655651
let (len, status, version, headers_len) = {
656-
let mut headers: [httparse::Header<'_>; MAX_HEADERS] =
657-
unsafe { mem::uninitialized() };
652+
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
658653
trace!(
659654
"Response.parse([Header; {}], [u8; {}])",
660655
headers.len(),

0 commit comments

Comments
 (0)