Skip to content

Commit 5255902

Browse files
committed
fix unsoundness in slice::from_ptr_range tests
1 parent 2319828 commit 5255902

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

library/core/src/slice/raw.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
33
use crate::array;
4+
use crate::intrinsics::is_aligned_and_not_null;
5+
use crate::mem;
6+
use crate::ops::Range;
47
use crate::ptr;
58

69
/// Forms a slice from a pointer and a length.
@@ -222,7 +225,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
222225
///
223226
/// use core::slice;
224227
///
225-
/// let x = [1, 2, 3, 4, 5];
228+
/// let x = [1, 2, 3];
226229
/// let range = x.as_ptr_range();
227230
///
228231
/// unsafe {
@@ -271,11 +274,11 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
271274
///
272275
/// use core::slice;
273276
///
274-
/// let mut x = [1, 2, 3, 4, 5];
277+
/// let x = [1, 2, 3];
275278
/// let range = x.as_mut_ptr_range();
276279
///
277280
/// unsafe {
278-
/// assert_eq!(slice::from_mut_ptr_range(range), &x);
281+
/// assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
279282
/// }
280283
/// ```
281284
///

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#![feature(pattern)]
3535
#![feature(sort_internals)]
3636
#![feature(slice_partition_at_index)]
37+
#![feature(slice_from_ptr_range)]
3738
#![feature(maybe_uninit_uninit_array)]
3839
#![feature(maybe_uninit_array_assume_init)]
3940
#![feature(maybe_uninit_extra)]

library/core/tests/slice.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::cell::Cell;
22
use core::cmp::Ordering;
33
use core::mem::MaybeUninit;
44
use core::result::Result::{Err, Ok};
5+
use core::slice;
56

67
#[test]
78
fn test_position() {
@@ -2234,20 +2235,21 @@ fn slice_split_array_mut_out_of_bounds() {
22342235
}
22352236

22362237
fn test_slice_from_ptr_range() {
2237-
let arr = [1, 2, 3];
2238+
let arr = ["foo".to_owned(), "bar".to_owned()];
22382239
let range = arr.as_ptr_range();
22392240
unsafe {
22402241
assert_eq!(slice::from_ptr_range(range), &arr);
22412242
}
22422243

2243-
let mut arr = ["foo".to_owned(), "bar".to_owned()];
2244+
let mut arr = [1, 2, 3];
22442245
let range = arr.as_mut_ptr_range();
22452246
unsafe {
2246-
assert_eq!(slice::from_mut_ptr_range(range), &arr);
2247+
assert_eq!(slice::from_mut_ptr_range(range), &mut [1, 2, 3]);
22472248
}
22482249

2249-
let range: Range<*const Vec<String>> = [].as_ptr_range();
2250+
let arr: [Vec<String>; 0] = [];
2251+
let range = arr.as_ptr_range();
22502252
unsafe {
2251-
assert_eq!(slice::from_ptr_range(range), &[] as &[Vec<String>]);
2253+
assert_eq!(slice::from_ptr_range(range), &arr);
22522254
}
22532255
}

0 commit comments

Comments
 (0)