seanmonstar / httparse

A push parser for the HTTP 1.x protocol in Rust.
https://docs.rs/httparse
Apache License 2.0
584 stars 114 forks source link

Need for `Request` to take a slice of `MaybeUninit<Header>` #95

Closed Soveu closed 3 years ago

Soveu commented 3 years ago

Context: hyper passes uninitialized array of httparse::Header to Request::new. This is undefined behavior and I discovered it while working on Replacing mem::uninitialized with MaybeUninit

I acknowledge that changing headers field will be a breaking change, because it is public, so is there a chance that a Request type copy could be implemented, but with headers: &mut [MaybeUninit<Header>]?

EDIT: I just realized that author of httparse is also author of hyper :sweat_smile:

seanmonstar commented 3 years ago

Yea, I allow this in hyper knowing that httparse can never look at the values, but it would be better to be explicit about it. It would be a breaking change, that's true. I haven't spent much time thinking about how to fix it in this repo, and instead just promise myself that the slice can never be inspected :shrug:

Soveu commented 3 years ago

Or we can just introduce a function that would just take buffer and parse immediately, it would fit for hyper

fn with_uninit_buffer_and_parse(headers: &mut [MaybeUninit<Header>]) -> (Request, Result<usize>)

I have already adjusted parse_headers_iter to use this type