Skip to content

Commit c1cda01

Browse files
authored
Rollup merge of rust-lang#68701 - amosonn:patch-2, r=RalfJung
Improve #Safety of various methods in core::ptr For `read`, `read_unaligned`,`read_volatile`, `replace`, and `drop_in_place`: - The value they point to must be properly initialized For `replace`, additionally: - The pointer must be readable
2 parents 78deb93 + 943e653 commit c1cda01

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/libcore/ptr/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,13 @@ mod mut_ptr;
119119
///
120120
/// Behavior is undefined if any of the following conditions are violated:
121121
///
122-
/// * `to_drop` must be [valid] for reads.
122+
/// * `to_drop` must be [valid] for both reads and writes.
123123
///
124124
/// * `to_drop` must be properly aligned.
125125
///
126+
/// * The value `to_drop` points to must be valid for dropping, which may mean it must uphold
127+
/// additional invariants - this is type-dependent.
128+
///
126129
/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
127130
/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
128131
/// foo` counts as a use because it will cause the value to be dropped
@@ -289,7 +292,7 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
289292
///
290293
/// Behavior is undefined if any of the following conditions are violated:
291294
///
292-
/// * Both `x` and `y` must be [valid] for reads and writes.
295+
/// * Both `x` and `y` must be [valid] for both reads and writes.
293296
///
294297
/// * Both `x` and `y` must be properly aligned.
295298
///
@@ -355,7 +358,7 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
355358
///
356359
/// Behavior is undefined if any of the following conditions are violated:
357360
///
358-
/// * Both `x` and `y` must be [valid] for reads and writes of `count *
361+
/// * Both `x` and `y` must be [valid] for both reads and writes of `count *
359362
/// size_of::<T>()` bytes.
360363
///
361364
/// * Both `x` and `y` must be properly aligned.
@@ -471,10 +474,12 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
471474
///
472475
/// Behavior is undefined if any of the following conditions are violated:
473476
///
474-
/// * `dst` must be [valid] for writes.
477+
/// * `dst` must be [valid] for both reads and writes.
475478
///
476479
/// * `dst` must be properly aligned.
477480
///
481+
/// * `dst` must point to a properly initialized value of type `T`.
482+
///
478483
/// Note that even if `T` has size `0`, the pointer must be non-NULL and properly aligned.
479484
///
480485
/// [valid]: ../ptr/index.html#safety
@@ -514,6 +519,8 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
514519
/// * `src` must be properly aligned. Use [`read_unaligned`] if this is not the
515520
/// case.
516521
///
522+
/// * `src` must point to a properly initialized value of type `T`.
523+
///
517524
/// Note that even if `T` has size `0`, the pointer must be non-NULL and properly aligned.
518525
///
519526
/// # Examples
@@ -628,6 +635,8 @@ pub unsafe fn read<T>(src: *const T) -> T {
628635
///
629636
/// * `src` must be [valid] for reads.
630637
///
638+
/// * `src` must point to a properly initialized value of type `T`.
639+
///
631640
/// Like [`read`], `read_unaligned` creates a bitwise copy of `T`, regardless of
632641
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
633642
/// value and the value at `*src` can [violate memory safety][read-ownership].
@@ -922,6 +931,8 @@ pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
922931
///
923932
/// * `src` must be properly aligned.
924933
///
934+
/// * `src` must point to a properly initialized value of type `T`.
935+
///
925936
/// Like [`read`], `read_volatile` creates a bitwise copy of `T`, regardless of
926937
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
927938
/// value and the value at `*src` can [violate memory safety][read-ownership].

0 commit comments

Comments
 (0)