Auto merge of #37041 - tbu-:pr_less_sizet_casts, r=alexcrichton

Use less `size_t` casts in libstd since it's now defined as `usize`
This commit is contained in:
bors 2016-10-08 15:59:49 -07:00 committed by GitHub
commit 19ac57926a
8 changed files with 32 additions and 38 deletions

View File

@ -229,7 +229,7 @@ mod prim_unit { }
/// ///
/// fn main() { /// fn main() {
/// unsafe { /// unsafe {
/// let my_num: *mut i32 = libc::malloc(mem::size_of::<i32>() as libc::size_t) as *mut i32; /// let my_num: *mut i32 = libc::malloc(mem::size_of::<i32>()) as *mut i32;
/// if my_num.is_null() { /// if my_num.is_null() {
/// panic!("failed to allocate memory"); /// panic!("failed to allocate memory");
/// } /// }

View File

@ -11,7 +11,7 @@
#![unstable(reason = "not public", issue = "0", feature = "fd")] #![unstable(reason = "not public", issue = "0", feature = "fd")]
use io::{self, Read}; use io::{self, Read};
use libc::{self, c_int, size_t, c_void}; use libc::{self, c_int, c_void};
use mem; use mem;
use sync::atomic::{AtomicBool, Ordering}; use sync::atomic::{AtomicBool, Ordering};
use sys::cvt; use sys::cvt;
@ -40,7 +40,7 @@ impl FileDesc {
let ret = cvt(unsafe { let ret = cvt(unsafe {
libc::read(self.fd, libc::read(self.fd,
buf.as_mut_ptr() as *mut c_void, buf.as_mut_ptr() as *mut c_void,
buf.len() as size_t) buf.len())
})?; })?;
Ok(ret as usize) Ok(ret as usize)
} }
@ -54,7 +54,7 @@ impl FileDesc {
let ret = cvt(unsafe { let ret = cvt(unsafe {
libc::write(self.fd, libc::write(self.fd,
buf.as_ptr() as *const c_void, buf.as_ptr() as *const c_void,
buf.len() as size_t) buf.len())
})?; })?;
Ok(ret as usize) Ok(ret as usize)
} }

View File

@ -669,7 +669,7 @@ pub fn readlink(p: &Path) -> io::Result<PathBuf> {
loop { loop {
let buf_read = cvt(unsafe { let buf_read = cvt(unsafe {
libc::readlink(p, buf.as_mut_ptr() as *mut _, buf.capacity() as libc::size_t) libc::readlink(p, buf.as_mut_ptr() as *mut _, buf.capacity())
})? as usize; })? as usize;
unsafe { buf.set_len(buf_read); } unsafe { buf.set_len(buf_read); }

View File

@ -18,7 +18,7 @@ pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
libc::memchr( libc::memchr(
haystack.as_ptr() as *const libc::c_void, haystack.as_ptr() as *const libc::c_void,
needle as libc::c_int, needle as libc::c_int,
haystack.len() as libc::size_t) haystack.len())
}; };
if p.is_null() { if p.is_null() {
None None
@ -39,7 +39,7 @@ pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
libc::memrchr( libc::memrchr(
haystack.as_ptr() as *const libc::c_void, haystack.as_ptr() as *const libc::c_void,
needle as libc::c_int, needle as libc::c_int,
haystack.len() as libc::size_t) haystack.len())
}; };
if p.is_null() { if p.is_null() {
None None

View File

@ -83,7 +83,7 @@ pub fn init() {
unsafe { unsafe {
libc::write(libc::STDERR_FILENO, libc::write(libc::STDERR_FILENO,
msg.as_ptr() as *const libc::c_void, msg.as_ptr() as *const libc::c_void,
msg.len() as libc::size_t); msg.len());
intrinsics::abort(); intrinsics::abort();
} }
} }

View File

@ -94,7 +94,7 @@ pub fn error_string(errno: i32) -> String {
let p = buf.as_mut_ptr(); let p = buf.as_mut_ptr();
unsafe { unsafe {
if strerror_r(errno as c_int, p, buf.len() as libc::size_t) < 0 { if strerror_r(errno as c_int, p, buf.len()) < 0 {
panic!("strerror_r failure"); panic!("strerror_r failure");
} }
@ -108,7 +108,7 @@ pub fn getcwd() -> io::Result<PathBuf> {
loop { loop {
unsafe { unsafe {
let ptr = buf.as_mut_ptr() as *mut libc::c_char; let ptr = buf.as_mut_ptr() as *mut libc::c_char;
if !libc::getcwd(ptr, buf.capacity() as libc::size_t).is_null() { if !libc::getcwd(ptr, buf.capacity()).is_null() {
let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len(); let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len();
buf.set_len(len); buf.set_len(len);
buf.shrink_to_fit(); buf.shrink_to_fit();
@ -200,21 +200,20 @@ pub fn current_exe() -> io::Result<PathBuf> {
libc::KERN_PROC as c_int, libc::KERN_PROC as c_int,
libc::KERN_PROC_PATHNAME as c_int, libc::KERN_PROC_PATHNAME as c_int,
-1 as c_int]; -1 as c_int];
let mut sz: libc::size_t = 0; let mut sz = 0;
cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint, cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
ptr::null_mut(), &mut sz, ptr::null_mut(), ptr::null_mut(), &mut sz, ptr::null_mut(), 0))?;
0 as libc::size_t))?;
if sz == 0 { if sz == 0 {
return Err(io::Error::last_os_error()) return Err(io::Error::last_os_error())
} }
let mut v: Vec<u8> = Vec::with_capacity(sz as usize); let mut v: Vec<u8> = Vec::with_capacity(sz);
cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint, cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
v.as_mut_ptr() as *mut libc::c_void, &mut sz, v.as_mut_ptr() as *mut libc::c_void, &mut sz,
ptr::null_mut(), 0 as libc::size_t))?; ptr::null_mut(), 0))?;
if sz == 0 { if sz == 0 {
return Err(io::Error::last_os_error()); return Err(io::Error::last_os_error());
} }
v.set_len(sz as usize - 1); // chop off trailing NUL v.set_len(sz - 1); // chop off trailing NUL
Ok(PathBuf::from(OsString::from_vec(v))) Ok(PathBuf::from(OsString::from_vec(v)))
} }
} }
@ -488,7 +487,7 @@ pub fn home_dir() -> Option<PathBuf> {
buf: &mut Vec<c_char>) -> Option<()> { buf: &mut Vec<c_char>) -> Option<()> {
let mut result = ptr::null_mut(); let mut result = ptr::null_mut();
match libc::getpwuid_r(me, passwd, buf.as_mut_ptr(), match libc::getpwuid_r(me, passwd, buf.as_mut_ptr(),
buf.capacity() as libc::size_t, buf.capacity(),
&mut result) { &mut result) {
0 if !result.is_null() => Some(()), 0 if !result.is_null() => Some(()),
_ => None _ => None
@ -501,7 +500,7 @@ pub fn home_dir() -> Option<PathBuf> {
// getpwuid_r semantics is different on Illumos/Solaris: // getpwuid_r semantics is different on Illumos/Solaris:
// http://illumos.org/man/3c/getpwuid_r // http://illumos.org/man/3c/getpwuid_r
let result = libc::getpwuid_r(me, passwd, buf.as_mut_ptr(), let result = libc::getpwuid_r(me, passwd, buf.as_mut_ptr(),
buf.capacity() as libc::size_t); buf.capacity());
if result.is_null() { None } else { Some(()) } if result.is_null() { None } else { Some(()) }
} }

View File

@ -97,8 +97,8 @@ mod imp {
// full entropy pool // full entropy pool
let reader = File::open("/dev/urandom").expect("Unable to open /dev/urandom"); let reader = File::open("/dev/urandom").expect("Unable to open /dev/urandom");
let mut reader_rng = ReaderRng::new(reader); let mut reader_rng = ReaderRng::new(reader);
reader_rng.fill_bytes(& mut v[read..]); reader_rng.fill_bytes(&mut v[read..]);
read += v.len() as usize; read += v.len();
} else { } else {
panic!("unexpected getrandom error: {}", err); panic!("unexpected getrandom error: {}", err);
} }
@ -281,7 +281,7 @@ mod imp {
} }
fn fill_bytes(&mut self, v: &mut [u8]) { fn fill_bytes(&mut self, v: &mut [u8]) {
let ret = unsafe { let ret = unsafe {
SecRandomCopyBytes(kSecRandomDefault, v.len() as size_t, SecRandomCopyBytes(kSecRandomDefault, v.len(),
v.as_mut_ptr()) v.as_mut_ptr())
}; };
if ret == -1 { if ret == -1 {

View File

@ -53,7 +53,7 @@ impl Thread {
let stack_size = cmp::max(stack, min_stack_size(&attr)); let stack_size = cmp::max(stack, min_stack_size(&attr));
match pthread_attr_setstacksize(&mut attr, match pthread_attr_setstacksize(&mut attr,
stack_size as libc::size_t) { stack_size) {
0 => {} 0 => {}
n => { n => {
assert_eq!(n, libc::EINVAL); assert_eq!(n, libc::EINVAL);
@ -64,7 +64,6 @@ impl Thread {
let page_size = os::page_size(); let page_size = os::page_size();
let stack_size = (stack_size + page_size - 1) & let stack_size = (stack_size + page_size - 1) &
(-(page_size as isize - 1) as usize - 1); (-(page_size as isize - 1) as usize - 1);
let stack_size = stack_size as libc::size_t;
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, assert_eq!(libc::pthread_attr_setstacksize(&mut attr,
stack_size), 0); stack_size), 0);
} }
@ -264,12 +263,8 @@ pub mod guard {
// Rellocate the last page of the stack. // Rellocate the last page of the stack.
// This ensures SIGBUS will be raised on // This ensures SIGBUS will be raised on
// stack overflow. // stack overflow.
let result = mmap(stackaddr, let result = mmap(stackaddr, psize, PROT_NONE,
psize as libc::size_t, MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
PROT_NONE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
-1,
0);
if result != stackaddr || result == MAP_FAILED { if result != stackaddr || result == MAP_FAILED {
panic!("failed to allocate a guard page"); panic!("failed to allocate a guard page");
@ -293,8 +288,8 @@ pub mod guard {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub unsafe fn current() -> Option<usize> { pub unsafe fn current() -> Option<usize> {
Some((libc::pthread_get_stackaddr_np(libc::pthread_self()) as libc::size_t - Some((libc::pthread_get_stackaddr_np(libc::pthread_self()) as usize -
libc::pthread_get_stacksize_np(libc::pthread_self())) as usize) libc::pthread_get_stacksize_np(libc::pthread_self())))
} }
#[cfg(any(target_os = "openbsd", target_os = "bitrig"))] #[cfg(any(target_os = "openbsd", target_os = "bitrig"))]
@ -306,10 +301,10 @@ pub mod guard {
let extra = if cfg!(target_os = "bitrig") {3} else {1} * os::page_size(); let extra = if cfg!(target_os = "bitrig") {3} else {1} * os::page_size();
Some(if libc::pthread_main_np() == 1 { Some(if libc::pthread_main_np() == 1 {
// main thread // main thread
current_stack.ss_sp as usize - current_stack.ss_size as usize + extra current_stack.ss_sp as usize - current_stack.ss_size + extra
} else { } else {
// new thread // new thread
current_stack.ss_sp as usize - current_stack.ss_size as usize current_stack.ss_sp as usize - current_stack.ss_size
}) })
} }
@ -335,11 +330,11 @@ pub mod guard {
&mut size), 0); &mut size), 0);
ret = if cfg!(target_os = "freebsd") { ret = if cfg!(target_os = "freebsd") {
Some(stackaddr as usize - guardsize as usize) Some(stackaddr as usize - guardsize)
} else if cfg!(target_os = "netbsd") { } else if cfg!(target_os = "netbsd") {
Some(stackaddr as usize) Some(stackaddr as usize)
} else { } else {
Some(stackaddr as usize + guardsize as usize) Some(stackaddr as usize + guardsize)
}; };
} }
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0); assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
@ -358,8 +353,8 @@ fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
weak!(fn __pthread_get_minstack(*const libc::pthread_attr_t) -> libc::size_t); weak!(fn __pthread_get_minstack(*const libc::pthread_attr_t) -> libc::size_t);
match __pthread_get_minstack.get() { match __pthread_get_minstack.get() {
None => libc::PTHREAD_STACK_MIN as usize, None => libc::PTHREAD_STACK_MIN,
Some(f) => unsafe { f(attr) as usize }, Some(f) => unsafe { f(attr) },
} }
} }
@ -368,7 +363,7 @@ fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
#[cfg(all(not(target_os = "linux"), #[cfg(all(not(target_os = "linux"),
not(target_os = "netbsd")))] not(target_os = "netbsd")))]
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
libc::PTHREAD_STACK_MIN as usize libc::PTHREAD_STACK_MIN
} }
#[cfg(target_os = "netbsd")] #[cfg(target_os = "netbsd")]