Skip to content

Commit

Permalink
Rollup merge of rust-lang#58536 - xfix:remove-ub-in-pointer-tests, r=…
Browse files Browse the repository at this point in the history
…RalfJung

Remove UB in pointer tests

UB found by Miri.
  • Loading branch information
kennytm authored Feb 18, 2019
2 parents 6d2cc83 + 0cf1a91 commit e3f75bf
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/libcore/tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ fn test() {
}

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

let q = unsafe { p.offset(1) };
let q = p.wrapping_offset(1);
assert!(!q.is_null());

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

let mq = unsafe { mp.offset(1) };
let mq = mp.wrapping_offset(1);
assert!(!mq.is_null());

// Pointers to unsized types -- slices
Expand Down Expand Up @@ -208,7 +207,6 @@ fn test_ptr_addition() {
}

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

while m_ptr >= m_start {
loop {
*m_ptr += *m_ptr;
if m_ptr == m_start {
break;
}
m_ptr = m_ptr.offset(-1);
}

Expand Down

0 comments on commit e3f75bf

Please sign in to comment.