Skip to content

Commit 8a37c9a

Browse files
committed
update the saftey preconditions of from_raw_parts
they now reflect the fact that zero-capacity collections do not allocate fixes #119304
1 parent 5ad98b4 commit 8a37c9a

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

library/alloc/src/string.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,9 @@ impl String {
913913
/// This is highly unsafe, due to the number of invariants that aren't
914914
/// checked:
915915
///
916-
/// * The memory at `buf` needs to have been previously allocated by the
917-
/// same allocator the standard library uses, with a required alignment of exactly 1.
916+
/// * unless `capacity` is 0, `buf` must have been allocated using the global allocator with an alignment of 1 and a capacity of `capacity`.
917+
/// * `buf` must not be null.
918918
/// * `length` needs to be less than or equal to `capacity`.
919-
/// * `capacity` needs to be the correct value.
920919
/// * The first `length` bytes at `buf` need to be valid UTF-8.
921920
///
922921
/// Violating these may cause problems like corrupting the allocator's

library/alloc/src/vec/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,9 @@ impl<T> Vec<T> {
503503
/// This is highly unsafe, due to the number of invariants that aren't
504504
/// checked:
505505
///
506-
/// * `ptr` must have been allocated using the global allocator, such as via
506+
/// * unless `capacity` is 0 or `T` has size 0, `ptr` must have been allocated using the global allocator, such as via
507507
/// the [`alloc::alloc`] function.
508+
/// * `ptr` must not be null.
508509
/// * `T` needs to have the same alignment as what `ptr` was allocated with.
509510
/// (`T` having a less strict alignment is not sufficient, the alignment really
510511
/// needs to be equal to satisfy the [`dealloc`] requirement that memory must be
@@ -514,12 +515,12 @@ impl<T> Vec<T> {
514515
/// alignment, [`dealloc`] must be called with the same layout `size`.)
515516
/// * `length` needs to be less than or equal to `capacity`.
516517
/// * The first `length` values must be properly initialized values of type `T`.
517-
/// * `capacity` needs to be the capacity that the pointer was allocated with.
518+
/// * `capacity` needs to be the capacity that the pointer was allocated with, or 0 in the case of a dangling pointer.
518519
/// * The allocated size in bytes must be no larger than `isize::MAX`.
519520
/// See the safety documentation of [`pointer::offset`].
520521
///
521522
/// These requirements are always upheld by any `ptr` that has been allocated
522-
/// via `Vec<T>`. Other allocation sources are allowed if the invariants are
523+
/// via `Vec<T>`. Note that a `Vec` of capacity 0 does not allocate. Other allocation sources are allowed if the invariants are
523524
/// upheld.
524525
///
525526
/// Violating these may cause problems like corrupting the allocator's
@@ -724,7 +725,7 @@ impl<T, A: Allocator> Vec<T, A> {
724725
/// This is highly unsafe, due to the number of invariants that aren't
725726
/// checked:
726727
///
727-
/// * `ptr` must be [*currently allocated*] via the given allocator `alloc`.
728+
/// * unless `capacity` is 0, `ptr` must be [*currently allocated*] via the given allocator `alloc`.
728729
/// * `T` needs to have the same alignment as what `ptr` was allocated with.
729730
/// (`T` having a less strict alignment is not sufficient, the alignment really
730731
/// needs to be equal to satisfy the [`dealloc`] requirement that memory must be
@@ -739,7 +740,7 @@ impl<T, A: Allocator> Vec<T, A> {
739740
/// See the safety documentation of [`pointer::offset`].
740741
///
741742
/// These requirements are always upheld by any `ptr` that has been allocated
742-
/// via `Vec<T, A>`. Other allocation sources are allowed if the invariants are
743+
/// via `Vec<T, A>`. Note that a `Vec` of capacity 0 does not allocate. Other allocation sources are allowed if the invariants are
743744
/// upheld.
744745
///
745746
/// Violating these may cause problems like corrupting the allocator's

0 commit comments

Comments
 (0)