tokio-rs / bytes

Utilities for working with bytes
MIT License
1.91k stars 288 forks source link

reserve_inner over allocates which can lead to a OOM conidition #559

Closed schultetwin1 closed 2 years ago

schultetwin1 commented 2 years ago

Summary

reseve_inner will double the size of the underlying shared vector instead of just extending the BytesMut buffer in place if a call to BytesMut::reserve would fit inside the current allocated shared buffer. If this happens repeatedly you will eventually attempt to allocate a buffer that is too large.

Repro

fn reserve_in_arc_unique_does_not_overallocate_after_split() {
    let mut bytes = BytesMut::from(LONG);
    let orig_capacity = bytes.capacity();
    drop(bytes.split_off(LONG.len() / 2));
    let new_capacity = bytes.capacity();
    bytes.reserve(orig_capacity - new_capacity);
    assert_eq!(bytes.capacity(), orig_capacity);
}