Skip to content

Commit db5f005

Browse files
authored
Rollup merge of #104568 - RalfJung:realloc, r=Amanieu
clarify that realloc refreshes pointer provenance even when the allocation remains in-place This [matches what C does](https://en.cppreference.com/w/c/memory/realloc): > The original pointer ptr is invalidated and any access to it is undefined behavior (even if reallocation was in-place). Cc `@rust-lang/wg-allocators`
2 parents 820a415 + d26659d commit db5f005

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

library/core/src/alloc/global.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ pub unsafe trait GlobalAlloc {
208208
///
209209
/// If this returns a non-null pointer, then ownership of the memory block
210210
/// referenced by `ptr` has been transferred to this allocator.
211-
/// The memory may or may not have been deallocated, and should be
212-
/// considered unusable. The new memory block is allocated with `layout`,
213-
/// but with the `size` updated to `new_size`. This new layout should be
211+
/// Any access to the old `ptr` is Undefined Behavior, even if the
212+
/// allocation remained in-place. The newly returned pointer is the only valid pointer
213+
/// for accessing this memory now.
214+
/// The new memory block is allocated with `layout`,
215+
/// but with the `size` updated to `new_size`. This new layout must be
214216
/// used when deallocating the new memory block with `dealloc`. The range
215217
/// `0..min(layout.size(), new_size)` of the new memory block is
216218
/// guaranteed to have the same values as the original block.

library/core/src/alloc/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ pub unsafe trait Allocator {
169169
/// this, the allocator may extend the allocation referenced by `ptr` to fit the new layout.
170170
///
171171
/// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been
172-
/// transferred to this allocator. The memory may or may not have been freed, and should be
173-
/// considered unusable.
172+
/// transferred to this allocator. Any access to the old `ptr` is Undefined Behavior, even if the
173+
/// allocation was grown in-place. The newly returned pointer is the only valid pointer
174+
/// for accessing this memory now.
174175
///
175176
/// If this method returns `Err`, then ownership of the memory block has not been transferred to
176177
/// this allocator, and the contents of the memory block are unaltered.
@@ -295,8 +296,9 @@ pub unsafe trait Allocator {
295296
/// this, the allocator may shrink the allocation referenced by `ptr` to fit the new layout.
296297
///
297298
/// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been
298-
/// transferred to this allocator. The memory may or may not have been freed, and should be
299-
/// considered unusable.
299+
/// transferred to this allocator. Any access to the old `ptr` is Undefined Behavior, even if the
300+
/// allocation was shrunk in-place. The newly returned pointer is the only valid pointer
301+
/// for accessing this memory now.
300302
///
301303
/// If this method returns `Err`, then ownership of the memory block has not been transferred to
302304
/// this allocator, and the contents of the memory block are unaltered.

0 commit comments

Comments
 (0)