Skip to content

Commit f6247ff

Browse files
committed
clarify how write_bytes can lead to UB due to invalid values
1 parent c4693bc commit f6247ff

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

library/core/src/intrinsics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2550,10 +2550,10 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
25502550
///
25512551
/// * `dst` must be properly aligned.
25522552
///
2553-
/// Additionally, the caller must ensure that writing `count *
2554-
/// size_of::<T>()` bytes to the given region of memory results in a valid
2555-
/// value of `T`. Using a region of memory typed as a `T` that contains an
2556-
/// invalid value of `T` is undefined behavior.
2553+
/// Additionally, note that changing `*dst` in this way can lead to undefined behavior later if the
2554+
/// written bytes are not a valid representation of some `T`. For instance, if `dst: *mut bool`, a
2555+
/// `dst.write_bytes(0xFFu8, 1)` followed by `dst.read()` is undefined behavior since the `read`
2556+
/// tries to construct a `bool` value from `0xFF` which does not represent any `bool`.
25572557
///
25582558
/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
25592559
/// `0`, the pointer must be non-null and properly aligned.

0 commit comments

Comments
 (0)