std::vec: Add UB check in from_parts_in

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
xizheyin 2025-07-13 16:18:56 +08:00
parent 090c177003
commit 48caa5f889
No known key found for this signature in database
GPG key ID: 0A0D90BE99CEDEAD

View file

@ -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 } }
}