Skip to content

Commit 48caa5f

Browse files
committed
std::vec: Add UB check in from_parts_in
Signed-off-by: xizheyin <[email protected]>
1 parent 090c177 commit 48caa5f

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,11 @@ impl<T, A: Allocator> Vec<T, A> {
11791179
#[unstable(feature = "allocator_api", reason = "new API", issue = "32838")]
11801180
// #[unstable(feature = "box_vec_non_null", issue = "130364")]
11811181
pub unsafe fn from_parts_in(ptr: NonNull<T>, length: usize, capacity: usize, alloc: A) -> Self {
1182+
ub_checks::assert_unsafe_precondition!(
1183+
check_library_ub,
1184+
"Vec::from_parts_in requires that length <= capacity",
1185+
(length: usize = length, capacity: usize = capacity) => length <= capacity
1186+
);
11821187
unsafe { Vec { buf: RawVec::from_nonnull_in(ptr, capacity, alloc), len: length } }
11831188
}
11841189

0 commit comments

Comments
 (0)