Skip to content

Commit 1cab09e

Browse files
committed
std::vec: Upgrade debug_assert to UB check in set_len
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 10866f4 commit 1cab09e

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
#![feature(try_trait_v2)]
154154
#![feature(try_with_capacity)]
155155
#![feature(tuple_trait)]
156+
#![feature(ub_checks)]
156157
#![feature(unicode_internals)]
157158
#![feature(unsize)]
158159
#![feature(unwrap_infallible)]

library/alloc/src/vec/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
6464
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
6565
use core::ptr::{self, NonNull};
6666
use core::slice::{self, SliceIndex};
67-
use core::{fmt, intrinsics};
67+
use core::{fmt, intrinsics, ub_checks};
6868

6969
#[stable(feature = "extract_if", since = "1.87.0")]
7070
pub use self::extract_if::ExtractIf;
@@ -1950,7 +1950,11 @@ impl<T, A: Allocator> Vec<T, A> {
19501950
#[inline]
19511951
#[stable(feature = "rust1", since = "1.0.0")]
19521952
pub unsafe fn set_len(&mut self, new_len: usize) {
1953-
debug_assert!(new_len <= self.capacity());
1953+
ub_checks::assert_unsafe_precondition!(
1954+
check_library_ub,
1955+
"Vec::set_len requires that new_len <= capacity()",
1956+
(new_len: usize = new_len, capacity: usize = self.capacity()) => new_len <= capacity
1957+
);
19541958

19551959
self.len = new_len;
19561960
}

0 commit comments

Comments
 (0)