Skip to content

Fixes the is_null method in PtrExt for fat pointers. #23163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,12 @@ impl<T: ?Sized> PtrExt for *const T {

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn is_null(self) -> bool { self == 0 as *const T }
fn is_null(self) -> bool {
unsafe {
let ptr_ptr: *const usize = mem::transmute(&self);
*ptr_ptr == 0
}
}

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -347,7 +352,7 @@ impl<T: ?Sized> PtrExt for *mut T {

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn is_null(self) -> bool { self == 0 as *mut T }
fn is_null(self) -> bool { (self as *const T).is_null() }

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
27 changes: 27 additions & 0 deletions src/libcoretest/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use core::ptr::*;
use core::mem;
use std::iter::repeat;
use core::slice;

#[test]
fn test() {
Expand Down Expand Up @@ -66,6 +67,17 @@ fn test_is_null() {

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

unsafe {
let sn = slice::from_raw_parts(null(), 0) as *const [u32];
assert!(sn.is_null());

let s = &[1, 2, 3] as *const [u32];
assert!(!s.is_null());
}

let s: *const str = "rust" as *const str;
assert!(!s.is_null());
}

#[test]
Expand All @@ -89,6 +101,15 @@ fn test_as_ref() {
let p: *const int = &u as *const _;
assert_eq!(p.as_ref().unwrap(), &2);
}

let sn = slice::from_raw_parts(null(), 0) as *const [u32];
assert_eq!(sn.as_ref(), None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is "6.1.3.2.3 Behavior considered undefined" because we put a null pointer inside a &[T]. It's just a test case, so I guess it can do no harm. I don't know what I want to say more than this is UB and rustc doesn't have to compile it into a sane test case if it doesn't want to.


let sl = &[1, 2, 3] as *const [u32];
assert_eq!(sl.as_ref().unwrap(), &[1, 2, 3]);

let s = "rust" as *const str;
assert_eq!(s.as_ref().unwrap(), "rust");
}
}

Expand All @@ -107,6 +128,12 @@ fn test_as_mut() {
let p: *mut int = &mut u as *mut _;
assert!(p.as_mut().unwrap() == &mut 2);
}

let sn = slice::from_raw_parts_mut(null_mut(), 0) as *mut [u32];
assert_eq!(sn.as_ref(), None);

let s = &mut [1, 2, 3] as *mut [u32];
assert_eq!(s.as_mut().unwrap(), &mut [1, 2, 3]);
}
}

Expand Down