From 6b4b0eda2980f09df18380c80f8ae6109ae70d83 Mon Sep 17 00:00:00 2001 From: Emily Crandall Fleischman Date: Fri, 12 Jul 2024 19:09:46 -0400 Subject: [PATCH 1/2] Fix `Bytes::is_unique` when created from shared `BytesMut` (#718) The `is_unique` entry in the vtable for `Bytes` created from a shared `BytesMut` just called the `shared_is_unique` function from the `bytes` module. However, that function dereferences the `data` argument` as `bytes::Shared`, but the actual underlying type is `bytes_mut::Shared`. --- src/bytes_mut.rs | 8 +++++++- tests/test_bytes.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index 282aaa710..2342077d2 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -1698,7 +1698,7 @@ unsafe fn rebuild_vec(ptr: *mut u8, mut len: usize, mut cap: usize, off: usize) static SHARED_VTABLE: Vtable = Vtable { clone: shared_v_clone, to_vec: shared_v_to_vec, - is_unique: crate::bytes::shared_is_unique, + is_unique: shared_v_is_unique, drop: shared_v_drop, }; @@ -1732,6 +1732,12 @@ unsafe fn shared_v_to_vec(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> V } } +unsafe fn shared_v_is_unique(data: &AtomicPtr<()>) -> bool { + let shared = data.load(Ordering::Acquire); + let ref_count = (*shared.cast::()).ref_count.load(Ordering::Relaxed); + ref_count == 1 +} + unsafe fn shared_v_drop(data: &mut AtomicPtr<()>, _ptr: *const u8, _len: usize) { data.with_mut(|shared| { release_shared(*shared as *mut Shared); diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 84c3d5a43..b2135905d 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -1172,3 +1172,12 @@ fn shared_is_unique() { drop(b); assert!(c.is_unique()); } + +#[test] +fn mut_shared_is_unique() { + let mut b = BytesMut::from(LONG); + let c = b.split().freeze(); + assert!(!c.is_unique()); + drop(b); + assert!(c.is_unique()); +} From fd13c7dcdb840653bf81294d141da77d3f1f9e1f Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Sat, 13 Jul 2024 07:45:33 +0000 Subject: [PATCH 2/2] chore: prepare bytes v1.6.1 (#720) --- CHANGELOG.md | 5 +++++ Cargo.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23357174b..ac5d8d5ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.6.1 (July 13, 2024) + +This release fixes a bug where `Bytes::is_unique` returns incorrect values when +the `Bytes` originates from a shared `BytesMut`. (#718) + # 1.6.0 (March 22, 2024) ### Added diff --git a/Cargo.toml b/Cargo.toml index 793582af1..ef44fbb9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ name = "bytes" # When releasing to crates.io: # - Update CHANGELOG.md. # - Create "v1.x.y" git tag. -version = "1.6.0" +version = "1.6.1" edition = "2018" rust-version = "1.39" license = "MIT"