Skip to content

"Transfer-Encoding" correction code causes a panic #2409

Closed
@inikulin

Description

@inikulin

Repro code:

use hyper::server::Server;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Client, Request, Response};
use std::convert::Infallible;

#[tokio::main]
async fn main() {
     let make_service = make_service_fn(|_conn| async {
        Ok::<_, Infallible>(service_fn(|_| async {
            Ok::<_, Infallible>(Response::builder().body(Body::empty()).unwrap())
        }))
    });

    let server = Server::bind(&"127.0.0.1:0".parse().unwrap()).serve(make_service);
    let addr = server.local_addr();

    tokio::spawn(async move {
        server.await.unwrap();
    });

    let req = Request::builder()
        .method("GET")
        .header("Transfer-Encoding", "foo")
        .uri(format!("http://{}", addr))
        .body(Body::from("foobar"))
        .unwrap();

    assert!(Client::new().request(req).await.is_ok());
}

This panics with "source slice length (3) does not match destination slice length (0)". Apparently the reason is that only capacity is set for the target buffer here:

let mut buf = BytesMut::with_capacity(new_cap);
, but the buffer itself is not filled, so slice copying fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-clientArea: client.A-http1Area: HTTP/1 specific.C-bugCategory: bug. Something is wrong. This is bad!E-easyEffort: easy. A task that would be a great starting point for a new contributor.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions