Skip to content

Commit e3f75bf

Browse files
authored
Rollup merge of rust-lang#58536 - xfix:remove-ub-in-pointer-tests, r=RalfJung
Remove UB in pointer tests UB found by Miri.
2 parents 6d2cc83 + 0cf1a91 commit e3f75bf

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/libcore/tests/ptr.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,17 @@ fn test() {
4040
}
4141

4242
#[test]
43-
#[cfg(not(miri))] // This test performs invalid OOB pointer arithmetic
4443
fn test_is_null() {
4544
let p: *const isize = null();
4645
assert!(p.is_null());
4746

48-
let q = unsafe { p.offset(1) };
47+
let q = p.wrapping_offset(1);
4948
assert!(!q.is_null());
5049

5150
let mp: *mut isize = null_mut();
5251
assert!(mp.is_null());
5352

54-
let mq = unsafe { mp.offset(1) };
53+
let mq = mp.wrapping_offset(1);
5554
assert!(!mq.is_null());
5655

5756
// Pointers to unsized types -- slices
@@ -208,7 +207,6 @@ fn test_ptr_addition() {
208207
}
209208

210209
#[test]
211-
#[cfg(not(miri))] // This test performs invalid OOB pointer arithmetic
212210
fn test_ptr_subtraction() {
213211
unsafe {
214212
let xs = vec![0,1,2,3,4,5,6,7,8,9];
@@ -224,8 +222,11 @@ fn test_ptr_subtraction() {
224222
let m_start = xs_mut.as_mut_ptr();
225223
let mut m_ptr = m_start.offset(9);
226224

227-
while m_ptr >= m_start {
225+
loop {
228226
*m_ptr += *m_ptr;
227+
if m_ptr == m_start {
228+
break;
229+
}
229230
m_ptr = m_ptr.offset(-1);
230231
}
231232

0 commit comments

Comments
 (0)