Skip to content

Impl of Buf with default copy_to_bytes impl could stack overflow #802

@discord9

Description

@discord9

i.e if one impl Buf trait for a struct and not impl copy_to_bytes code like this can stack over flow:

#[test]
fn test_buffer_overflow() {
    let parts: Vec<Bytes> = vec![];
    let buf = Buffer::from(parts);

    let mut ret = BytesMut::with_capacity(buf.len());
    ret.put(buf.clone());
}

in which Buffer impl Buf but not copy_to_bytes, so calling put(while ret.cap==0) will call src's copy_to_bytes which in turn (in default impl) will call BytesMut's put (also ret.cap==0), and so on, and so on.
Maybe provide a default impl for Buf's copy_to_bytes that not cause stack overflow? Although this is a small problem and easy to debug, but providing a more explictly notify could be better

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions