Skip to content

Commit bb81fb2

Browse files
committed
ptr::replace: make calls on ZST null ptr not UB
1 parent e22dab3 commit bb81fb2

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

library/core/src/ptr/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,13 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
15521552
is_zst: bool = T::IS_ZST,
15531553
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
15541554
);
1555+
if T::IS_ZST {
1556+
// `dst` may be valid for read and writes while also being null, in which case we cannot
1557+
// call `mem::replace`. However, we also don't have to actually do anything since there
1558+
// isn't actually any data to be copied anyway. All values of type `T` are
1559+
// bit-identical, so we can just return `src`` here.
1560+
return src;
1561+
}
15551562
mem::replace(&mut *dst, src)
15561563
}
15571564
}

0 commit comments

Comments
 (0)