Skip to content

Commit a7faf20

Browse files
committed
Avoid underflow
1 parent e375c8d commit a7faf20

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

library/core/src/slice/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3271,7 +3271,8 @@ impl<T> [T] {
32713271
#[inline]
32723272
#[unstable(feature = "slice_take", issue = "62280")]
32733273
pub fn take_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
3274-
self.take((self.len() - 1)..).map(|res| &res[0])
3274+
let i = self.len().checked_sub(1)?;
3275+
self.take(i..).map(|res| &res[0])
32753276
}
32763277

32773278
/// Returns a mutable reference to the last element of the slice,
@@ -3294,7 +3295,8 @@ impl<T> [T] {
32943295
#[inline]
32953296
#[unstable(feature = "slice_take", issue = "62280")]
32963297
pub fn take_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
3297-
self.take_mut((self.len() - 1)..).map(|res| &mut res[0])
3298+
let i = self.len().checked_sub(1)?;
3299+
self.take_mut(i..).map(|res| &mut res[0])
32983300
}
32993301
}
33003302

0 commit comments

Comments
 (0)