Skip to content

Commit 56d540e

Browse files
committed
Auto merge of #97053 - CAD97:realloc-clarification, r=dtolnay
Remove potentially misleading realloc parenthetical This parenthetical is problematic, because it suggests that the following is sound: ```rust let layout = Layout::new::<[u8; 32]>(); let p1 = alloc(layout); let p2 = realloc(p1, layout, 32); if p1 == p2 { p1.write([0; 32]); dealloc(p1, layout); } else { dealloc(p2, layout); } ``` At the very least, this isn't the case for [ANSI `realloc`](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). and [Windows `HeapReAlloc`](https://docs.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heaprealloc) is unclear at best (`HEAP_REALLOC_IN_PLACE_ONLY`'s description may imply that the old pointer may be used if `HEAP_REALLOC_IN_PLACE_ONLY` is provided). The conservative position is to just remove the parenthetical. cc `@rust-lang/wg-unsafe-code-guidelines` `@rust-lang/wg-allocators`
2 parents cdd74fc + 09dc24b commit 56d540e

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

library/core/src/alloc/global.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,9 @@ 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,
212-
/// and should be considered unusable (unless of course it was
213-
/// transferred back to the caller again via the return value of
214-
/// this method). The new memory block is allocated with `layout`, but
215-
/// with the `size` updated to `new_size`. This new layout should be
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
216214
/// used when deallocating the new memory block with `dealloc`. The range
217215
/// `0..min(layout.size(), new_size)` of the new memory block is
218216
/// guaranteed to have the same values as the original block.

library/core/src/alloc/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ pub unsafe trait Allocator {
161161
///
162162
/// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been
163163
/// transferred to this allocator. The memory may or may not have been freed, and should be
164-
/// considered unusable unless it was transferred back to the caller again via the return value
165-
/// of this method.
164+
/// considered unusable.
166165
///
167166
/// If this method returns `Err`, then ownership of the memory block has not been transferred to
168167
/// this allocator, and the contents of the memory block are unaltered.
@@ -288,8 +287,7 @@ pub unsafe trait Allocator {
288287
///
289288
/// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been
290289
/// transferred to this allocator. The memory may or may not have been freed, and should be
291-
/// considered unusable unless it was transferred back to the caller again via the return value
292-
/// of this method.
290+
/// considered unusable.
293291
///
294292
/// If this method returns `Err`, then ownership of the memory block has not been transferred to
295293
/// this allocator, and the contents of the memory block are unaltered.

0 commit comments

Comments
 (0)