You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix soundness hole in Ref::into_ref and into_mut (#721)
This commit implements the fix for #716 which will be released as a new
version in version trains 0.2, 0.3, 0.4, 0.5, 0.6, and 0.7. See #716 for
a description of the soundness hole and an explanation of why this fix
is chosen.
Unfortunately, due to dtolnay/trybuild#241, there is no way for us to
write a UI test that will detect a failure post-monomorphization, which
is when the code implemented in this change is designed to fail. I have
manually verified that unsound uses of these APIs now fail to compile.
Release 0.7.31.
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
5172
5201
#[allow(clippy::undocumented_unsafe_blocks)]
5173
5202
unsafeimpl<'a>ByteSlicefor&'amut[u8]{
5203
+
// SAFETY: If `&'b mut [u8]: 'a`, then the underlying memory is treated as
5204
+
// borrowed mutably for `'a` even if the slice itself is dropped.
5205
+
constINTO_REF_INTO_MUT_ARE_SOUND:bool = true;
5206
+
5174
5207
#[inline]
5175
5208
fnsplit_at(self,mid:usize) -> (Self,Self){
5176
5209
<[u8]>::split_at_mut(self, mid)
@@ -5181,6 +5214,16 @@ impl<'a> sealed::ByteSliceSealed for cell::Ref<'a, [u8]> {}
5181
5214
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
5182
5215
#[allow(clippy::undocumented_unsafe_blocks)]
5183
5216
unsafeimpl<'a>ByteSlicefor cell::Ref<'a,[u8]>{
5217
+
constINTO_REF_INTO_MUT_ARE_SOUND:bool = if !cfg!(doc){
5218
+
panic!("Ref::into_ref and Ref::into_mut are unsound when used with core::cell::Ref; see https://github.com/google/zerocopy/issues/716")
5219
+
}else{
5220
+
// When compiling documentation, allow the evaluation of this constant
5221
+
// to succeed. This doesn't represent a soundness hole - it just delays
5222
+
// any error to runtime. The reason we need this is that, otherwise,
5223
+
// `rustdoc` will fail when trying to document this item.
@@ -5191,6 +5234,16 @@ impl<'a> sealed::ByteSliceSealed for RefMut<'a, [u8]> {}
5191
5234
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
5192
5235
#[allow(clippy::undocumented_unsafe_blocks)]
5193
5236
unsafeimpl<'a>ByteSliceforRefMut<'a,[u8]>{
5237
+
constINTO_REF_INTO_MUT_ARE_SOUND:bool = if !cfg!(doc){
5238
+
panic!("Ref::into_ref and Ref::into_mut are unsound when used with core::cell::RefMut; see https://github.com/google/zerocopy/issues/716")
5239
+
}else{
5240
+
// When compiling documentation, allow the evaluation of this constant
5241
+
// to succeed. This doesn't represent a soundness hole - it just delays
5242
+
// any error to runtime. The reason we need this is that, otherwise,
5243
+
// `rustdoc` will fail when trying to document this item.
0 commit comments