Skip to content

Commit 090c177

Browse files
committed
std::vec: Add UB check in from_raw_parts_in
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 1cab09e commit 090c177

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
@@ -1058,6 +1058,11 @@ impl<T, A: Allocator> Vec<T, A> {
10581058
#[inline]
10591059
#[unstable(feature = "allocator_api", issue = "32838")]
10601060
pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self {
1061+
ub_checks::assert_unsafe_precondition!(
1062+
check_library_ub,
1063+
"Vec::from_raw_parts_in requires that length <= capacity",
1064+
(length: usize = length, capacity: usize = capacity) => length <= capacity
1065+
);
10611066
unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } }
10621067
}
10631068

0 commit comments

Comments
 (0)