Skip to content

Commit 5b79d31

Browse files
committed
Merge tag 'v1.11.1'
2 parents 804ee6d + 417dccd commit 5b79d31

5 files changed

Lines changed: 33 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.11.1 (February 3rd, 2026)
2+
3+
- Fix integer overflow in `BytesMut::reserve`
4+
15
# 1.11.0 (November 14th, 2025)
26

37
- Bump MSRV to 1.57 (#788)

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "bytes"
44
# When releasing to crates.io:
55
# - Update CHANGELOG.md.
66
# - Create "v1.x.y" git tag.
7-
version = "1.11.0"
7+
version = "1.11.1"
88
edition = "2021"
99
rust-version = "1.57"
1010
license = "MIT"

ci/miri.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ export MIRIFLAGS="-Zmiri-strict-provenance"
88

99
cargo miri test
1010
cargo miri test --target mips64-unknown-linux-gnuabi64
11+
12+
# run with wrapping integer overflow instead of panic
13+
cargo miri test --release

src/bytes_mut.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,15 @@ impl BytesMut {
697697

698698
let offset = self.ptr.as_ptr().offset_from(ptr) as usize;
699699

700+
let new_cap_plus_offset = match new_cap.checked_add(offset) {
701+
Some(new_cap_plus_offset) => new_cap_plus_offset,
702+
None if !allocate => return false,
703+
None => panic!("overflow"),
704+
};
705+
700706
// Compare the condition in the `kind == KIND_VEC` case above
701707
// for more details.
702-
if v_capacity >= new_cap + offset {
708+
if v_capacity >= new_cap_plus_offset {
703709
self.cap = new_cap;
704710
// no copy is necessary
705711
} else if v_capacity >= new_cap && offset >= len {
@@ -715,14 +721,12 @@ impl BytesMut {
715721
if !allocate {
716722
return false;
717723
}
718-
// calculate offset
719-
let off = (self.ptr.as_ptr() as usize) - (v.as_ptr() as usize);
720724

721725
// new_cap is calculated in terms of `BytesMut`, not the underlying
722726
// `Vec`, so it does not take the offset into account.
723727
//
724728
// Thus we have to manually add it here.
725-
new_cap = new_cap.checked_add(off).expect("overflow");
729+
new_cap = new_cap_plus_offset;
726730

727731
// The vector capacity is not sufficient. The reserve request is
728732
// asking for more than the initial buffer capacity. Allocate more
@@ -744,13 +748,13 @@ impl BytesMut {
744748
// the unused capacity of the vector is copied over to the new
745749
// allocation, so we need to ensure that we don't have any data we
746750
// care about in the unused capacity before calling `reserve`.
747-
debug_assert!(off + len <= v.capacity());
748-
v.set_len(off + len);
751+
debug_assert!(offset + len <= v.capacity());
752+
v.set_len(offset + len);
749753
v.reserve(new_cap - v.len());
750754

751755
// Update the info
752-
self.ptr = vptr(v.as_mut_ptr().add(off));
753-
self.cap = v.capacity() - off;
756+
self.ptr = vptr(v.as_mut_ptr().add(offset));
757+
self.cap = v.capacity() - offset;
754758
}
755759

756760
return true;

tests/test_bytes.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,3 +1707,16 @@ fn bytes_mut_put_bytes_specialization() {
17071707
// If allocation is reused, capacity should be equal to original vec capacity.
17081708
assert_eq!(bytes_mut.capacity(), capacity);
17091709
}
1710+
1711+
#[test]
1712+
#[should_panic]
1713+
fn bytes_mut_reserve_overflow() {
1714+
let mut a = BytesMut::from(&b"hello world"[..]);
1715+
let mut b = a.split_off(5);
1716+
// Ensure b becomes the unique owner of the backing storage
1717+
drop(a);
1718+
// Trigger overflow in new_cap + offset inside reserve
1719+
b.reserve(usize::MAX - 6);
1720+
// This call relies on the corrupted cap and may cause UB & HBO
1721+
b.put_u8(b'h');
1722+
}

0 commit comments

Comments
 (0)