diff options
| author | xizheyin <xizheyin@smail.nju.edu.cn> | 2025-07-13 16:18:56 +0800 |
|---|---|---|
| committer | xizheyin <xizheyin@smail.nju.edu.cn> | 2025-07-13 16:18:56 +0800 |
| commit | 48caa5f8897263da935ebdc2bbec8a95b5d3d025 (patch) | |
| tree | 792afa9e367c46e27092f76d2f742bdeacbf84f9 | |
| parent | 090c177003272da7d2d3b04702f35261d3952b72 (diff) | |
| download | rust-48caa5f8897263da935ebdc2bbec8a95b5d3d025.tar.gz rust-48caa5f8897263da935ebdc2bbec8a95b5d3d025.zip | |
std::vec: Add UB check in `from_parts_in`
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 6d44681fb28..50f8cc375bf 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1179,6 +1179,11 @@ impl<T, A: Allocator> Vec<T, A> { #[unstable(feature = "allocator_api", reason = "new API", issue = "32838")] // #[unstable(feature = "box_vec_non_null", issue = "130364")] pub unsafe fn from_parts_in(ptr: NonNull<T>, length: usize, capacity: usize, alloc: A) -> Self { + ub_checks::assert_unsafe_precondition!( + check_library_ub, + "Vec::from_parts_in requires that length <= capacity", + (length: usize = length, capacity: usize = capacity) => length <= capacity + ); unsafe { Vec { buf: RawVec::from_nonnull_in(ptr, capacity, alloc), len: length } } } |
