From dbe602bef41ffb8c79d36f95d2f32d3d1d8e276a Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sat, 10 Sep 2022 02:34:24 +0100 Subject: [PATCH 1/2] Make tests work under Miri again --- src/boxed.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/boxed.rs b/src/boxed.rs index 081ffee..63c88ea 100644 --- a/src/boxed.rs +++ b/src/boxed.rs @@ -174,13 +174,19 @@ impl From for BoxedString { } else { #[cfg(has_allocator)] { + // s.as_mut_ptr() borrows the string for &s[0..len] + // However, `allocator.grow()` copies the entire allocation - i.e. `&s[0..cap] + // if cap > len, Miri will throw an error as the read is out of bounds + // Under Miri, shrink the allocation to `len` to remove this mismatch and allow tests + // to run + #[cfg(miri)] + s.shrink_to_fit(); // TODO: Use String::into_raw_parts when stabilised, meanwhile let's get unsafe let len = s.len(); let cap = s.capacity(); #[allow(unsafe_code)] let ptr = unsafe { NonNull::new_unchecked(s.as_mut_ptr()) }; let old_layout = Layout::array::(cap).unwrap(); - use alloc::alloc::Allocator; let allocator = alloc::alloc::Global; if let Ok(aligned_ptr) = From 546fa9d618aec223f9f6a692db19510a1ca6cb04 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sat, 10 Sep 2022 02:42:59 +0100 Subject: [PATCH 2/2] Use `as_mut_vec` before getting pointer This borrows the underlying buffer for its full capacity rather than len --- src/boxed.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/boxed.rs b/src/boxed.rs index 63c88ea..45a5fea 100644 --- a/src/boxed.rs +++ b/src/boxed.rs @@ -174,19 +174,13 @@ impl From for BoxedString { } else { #[cfg(has_allocator)] { - // s.as_mut_ptr() borrows the string for &s[0..len] - // However, `allocator.grow()` copies the entire allocation - i.e. `&s[0..cap] - // if cap > len, Miri will throw an error as the read is out of bounds - // Under Miri, shrink the allocation to `len` to remove this mismatch and allow tests - // to run - #[cfg(miri)] - s.shrink_to_fit(); // TODO: Use String::into_raw_parts when stabilised, meanwhile let's get unsafe let len = s.len(); let cap = s.capacity(); #[allow(unsafe_code)] - let ptr = unsafe { NonNull::new_unchecked(s.as_mut_ptr()) }; + let ptr = unsafe { NonNull::new_unchecked(s.as_mut_vec().as_mut_ptr()) }; let old_layout = Layout::array::(cap).unwrap(); + use alloc::alloc::Allocator; let allocator = alloc::alloc::Global; if let Ok(aligned_ptr) =