Skip to content

Improve safety & usability of |size_t| and |ssize_t|. #28096

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove now-unnecessary casts of |usize| to |size_t|.
  • Loading branch information
briansmith committed Aug 30, 2015
commit 62aaa3024f4ba7dc7821da2f9b9f77aa1524c02f
2 changes: 1 addition & 1 deletion src/compiletest/raise_fd_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub unsafe fn raise_fd_limit() {
// Fetch the kern.maxfilesperproc value
let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC];
let mut maxfiles: libc::c_int = 0;
let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
let mut size = size_of_val(&maxfiles);
if sysctl(&mut mib[0], 2, &mut maxfiles as *mut _ as *mut _, &mut size,
null_mut(), 0) != 0 {
let err = io::Error::last_os_error();
Expand Down
10 changes: 5 additions & 5 deletions src/liballoc_jemalloc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,31 @@ fn align_to_flags(align: usize) -> c_int {
#[no_mangle]
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
let flags = align_to_flags(align);
unsafe { je_mallocx(size as size_t, flags) as *mut u8 }
unsafe { je_mallocx(size, flags) as *mut u8 }
}

#[no_mangle]
pub extern fn __rust_reallocate(ptr: *mut u8, _old_size: usize, size: usize,
align: usize) -> *mut u8 {
let flags = align_to_flags(align);
unsafe { je_rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8 }
unsafe { je_rallocx(ptr as *mut c_void, size, flags) as *mut u8 }
}

#[no_mangle]
pub extern fn __rust_reallocate_inplace(ptr: *mut u8, _old_size: usize,
size: usize, align: usize) -> usize {
let flags = align_to_flags(align);
unsafe { je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
unsafe { je_xallocx(ptr as *mut c_void, size, 0, flags) as usize }
}

#[no_mangle]
pub extern fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
let flags = align_to_flags(align);
unsafe { je_sdallocx(ptr as *mut c_void, old_size as size_t, flags) }
unsafe { je_sdallocx(ptr as *mut c_void, old_size, flags) }
}

#[no_mangle]
pub extern fn __rust_usable_size(size: usize, align: usize) -> usize {
let flags = align_to_flags(align);
unsafe { je_nallocx(size as size_t, flags) as usize }
unsafe { je_nallocx(size, flags) as usize }
}
10 changes: 4 additions & 6 deletions src/liballoc_system/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,16 @@ mod imp {

pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
if align <= MIN_ALIGN {
libc::malloc(size as libc::size_t) as *mut u8
libc::malloc(size) as *mut u8
} else {
#[cfg(target_os = "android")]
unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
memalign(align as libc::size_t, size as libc::size_t) as *mut u8
memalign(align, size) as *mut u8
}
#[cfg(not(target_os = "android"))]
unsafe fn more_aligned_malloc(size: usize, align: usize) -> *mut u8 {
let mut out = ptr::null_mut();
let ret = posix_memalign(&mut out,
align as libc::size_t,
size as libc::size_t);
let ret = posix_memalign(&mut out, align, size);
if ret != 0 {
ptr::null_mut()
} else {
Expand All @@ -110,7 +108,7 @@ mod imp {
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize,
align: usize) -> *mut u8 {
if align <= MIN_ALIGN {
libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
libc::realloc(ptr as *mut libc::c_void, size) as *mut u8
} else {
let new_ptr = allocate(size, align);
ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
Expand Down
8 changes: 2 additions & 6 deletions src/libflate/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Bytes {
unsafe {
let mut outsz: size_t = 0;
let res = tdefl_compress_mem_to_heap(bytes.as_ptr() as *const _,
bytes.len() as size_t,
&mut outsz,
flags);
bytes.len(), &mut outsz, flags);
assert!(!res.is_null());
Bytes {
ptr: Unique::new(res as *mut u8),
Expand All @@ -127,9 +125,7 @@ fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> Result<Bytes,Error> {
unsafe {
let mut outsz: size_t = 0;
let res = tinfl_decompress_mem_to_heap(bytes.as_ptr() as *const _,
bytes.len() as size_t,
&mut outsz,
flags);
bytes.len(), &mut outsz, flags);
if !res.is_null() {
Ok(Bytes {
ptr: Unique::new(res as *mut u8),
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_trans/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::str;

use libc;
use llvm::archive_ro::{ArchiveRO, Child};
use llvm::{self, ArchiveKind};
use rustc::metadata::loader::METADATA_FILENAME;
Expand Down Expand Up @@ -485,8 +484,7 @@ impl<'a> ArchiveBuilder<'a> {

let dst = self.config.dst.to_str().unwrap().as_bytes();
let dst = try!(CString::new(dst));
let r = llvm::LLVMRustWriteArchive(dst.as_ptr(),
members.len() as libc::size_t,
let r = llvm::LLVMRustWriteArchive(dst.as_ptr(), members.len(),
members.as_ptr(),
self.should_update_symbols,
kind);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
time(sess.time_passes(), &format!("ll link {}", name), || unsafe {
if !llvm::LLVMRustLinkInExternalBitcode(llmod,
ptr as *const libc::c_char,
bc_decoded.len() as libc::size_t) {
bc_decoded.len()) {
write::llvm_err(sess.diagnostic().handler(),
format!("failed to load bc of `{}`",
&name[..]));
Expand All @@ -115,7 +115,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
unsafe {
llvm::LLVMRustRunRestrictionPass(llmod,
ptr as *const *const libc::c_char,
arr.len() as libc::size_t);
arr.len());
}

if sess.no_landing_pads() {
Expand Down
9 changes: 3 additions & 6 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
(*renderer).codespan = Some(codespan);

let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
hoedown_document_render(document, ob, s.as_ptr(),
s.len() as libc::size_t);
hoedown_document_render(document, ob, s.as_ptr(), s.len());
hoedown_document_free(document);

hoedown_html_renderer_free(renderer);
Expand Down Expand Up @@ -435,8 +434,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
= tests as *mut _ as *mut libc::c_void;

let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
hoedown_document_render(document, ob, doc.as_ptr(),
doc.len() as libc::size_t);
hoedown_document_render(document, ob, doc.as_ptr(), doc.len());
hoedown_document_free(document);

hoedown_html_renderer_free(renderer);
Expand Down Expand Up @@ -556,8 +554,7 @@ pub fn plain_summary_line(md: &str) -> String {
(*renderer).normal_text = Some(normal_text);

let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
hoedown_document_render(document, ob, md.as_ptr(),
md.len() as libc::size_t);
hoedown_document_render(document, ob, md.as_ptr(), md.len());
hoedown_document_free(document);
let plain_slice = (*ob).as_bytes();
let plain = match str::from_utf8(plain_slice) {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ mod prim_unit { }
///
/// fn main() {
/// 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() {
/// panic!("failed to allocate memory");
/// }
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/rand/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ mod imp {
}
fn fill_bytes(&mut self, v: &mut [u8]) {
let ret = unsafe {
SecRandomCopyBytes(kSecRandomDefault, v.len() as size_t,
v.as_mut_ptr())
SecRandomCopyBytes(kSecRandomDefault, v.len(), v.as_mut_ptr())
};
if ret == -1 {
panic!("couldn't generate random bytes: {}",
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {

let data = unsafe {
try!(cvt_gai(getnameinfo(inner, len,
hostbuf.as_mut_ptr(), NI_MAXHOST as libc::size_t,
0 as *mut _, 0, 0)));
hostbuf.as_mut_ptr(), NI_MAXHOST, 0 as *mut _,
0, 0)));

CStr::from_ptr(hostbuf.as_ptr())
};
Expand Down
10 changes: 3 additions & 7 deletions src/libstd/sys/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use io;
use libc::{self, c_int, size_t, c_void};
use libc::{self, c_int, c_void};
use mem;
use sys::c;
use sys::cvt;
Expand All @@ -35,18 +35,14 @@ impl FileDesc {

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let ret = try!(cvt(unsafe {
libc::read(self.fd,
buf.as_mut_ptr() as *mut c_void,
buf.len() as size_t)
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, buf.len())
}));
Ok(ret as usize)
}

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let ret = try!(cvt(unsafe {
libc::write(self.fd,
buf.as_ptr() as *const c_void,
buf.len() as size_t)
libc::write(self.fd, buf.as_ptr() as *const c_void, buf.len())
}));
Ok(ret as usize)
}
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use os::unix::prelude::*;
use ffi::{CString, CStr, OsString, OsStr};
use fmt;
use io::{self, Error, ErrorKind, SeekFrom};
use libc::{self, c_int, size_t, off_t, c_char, mode_t};
use libc::{self, c_int, off_t, c_char, mode_t};
use mem;
use path::{Path, PathBuf};
use ptr;
Expand Down Expand Up @@ -477,7 +477,7 @@ pub fn readlink(p: &Path) -> io::Result<PathBuf> {

loop {
let buf_read = try!(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;

unsafe { buf.set_len(buf_read); }
Expand Down
12 changes: 5 additions & 7 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn error_string(errno: i32) -> String {

let p = buf.as_mut_ptr();
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");
}

Expand All @@ -97,7 +97,7 @@ pub fn getcwd() -> io::Result<PathBuf> {
loop {
unsafe {
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();
buf.set_len(len);
buf.shrink_to_fit();
Expand Down Expand Up @@ -193,14 +193,13 @@ pub fn current_exe() -> io::Result<PathBuf> {
-1 as c_int];
let mut sz: libc::size_t = 0;
let err = sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
ptr::null_mut(), &mut sz, ptr::null_mut(),
0 as libc::size_t);
ptr::null_mut(), &mut sz, ptr::null_mut(), 0);
if err != 0 { return Err(io::Error::last_os_error()); }
if sz == 0 { return Err(io::Error::last_os_error()); }
let mut v: Vec<u8> = Vec::with_capacity(sz as usize);
let err = sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
v.as_mut_ptr() as *mut libc::c_void, &mut sz,
ptr::null_mut(), 0 as libc::size_t);
ptr::null_mut(), 0);
if err != 0 { return Err(io::Error::last_os_error()); }
if sz == 0 { return Err(io::Error::last_os_error()); }
v.set_len(sz as usize - 1); // chop off trailing NUL
Expand Down Expand Up @@ -483,8 +482,7 @@ pub fn home_dir() -> Option<PathBuf> {
let mut passwd: c::passwd = mem::zeroed();
let mut result = 0 as *mut _;
match c::getpwuid_r(me, &mut passwd, buf.as_mut_ptr(),
buf.capacity() as libc::size_t,
&mut result) {
buf.capacity(), &mut result) {
0 if !result.is_null() => {}
_ => return None
}
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Thread {
assert_eq!(pthread_attr_init(&mut attr), 0);

let stack_size = cmp::max(stack, min_stack_size(&attr));
match pthread_attr_setstacksize(&mut attr, stack_size as libc::size_t) {
match pthread_attr_setstacksize(&mut attr, stack_size) {
0 => {}
n => {
assert_eq!(n, libc::EINVAL);
Expand All @@ -54,7 +54,7 @@ impl Thread {
let page_size = os::page_size();
let stack_size = (stack_size + page_size - 1) &
(-(page_size as isize - 1) as usize - 1);
let stack_size = stack_size as libc::size_t;
let stack_size = stack_size;
assert_eq!(pthread_attr_setstacksize(&mut attr, stack_size), 0);
}
};
Expand Down Expand Up @@ -238,7 +238,7 @@ pub mod guard {
// This ensures SIGBUS will be raised on
// stack overflow.
let result = mmap(stackaddr,
psize as libc::size_t,
psize,
PROT_NONE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
-1,
Expand All @@ -259,7 +259,7 @@ pub mod guard {
fn pthread_get_stackaddr_np(thread: pthread_t) -> *mut libc::c_void;
fn pthread_get_stacksize_np(thread: pthread_t) -> libc::size_t;
}
Some((pthread_get_stackaddr_np(pthread_self()) as libc::size_t -
Some((pthread_get_stackaddr_np(pthread_self()) -
pthread_get_stacksize_np(pthread_self())) as usize)
}

Expand Down
6 changes: 3 additions & 3 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl Thread {
// Round up to the next 64 kB because that's what the NT kernel does,
// might as well make it explicit.
let stack_size = (stack + 0xfffe) & (!0xfffe);
let ret = c::CreateThread(ptr::null_mut(), stack_size as libc::size_t,
thread_start, &*p as *const _ as *mut _,
0, ptr::null_mut());
let ret = c::CreateThread(ptr::null_mut(), stack_size, thread_start,
&*p as *const _ as *mut _, 0,
ptr::null_mut());

return if ret as usize == 0 {
Err(io::Error::last_os_error())
Expand Down
4 changes: 2 additions & 2 deletions src/test/auxiliary/allocator-dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub static mut HITS: usize = 0;
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
unsafe {
HITS += 1;
libc::malloc(size as libc::size_t) as *mut u8
libc::malloc(size) as *mut u8
}
}

Expand All @@ -39,7 +39,7 @@ pub extern fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
pub extern fn __rust_reallocate(ptr: *mut u8, old_size: usize, size: usize,
align: usize) -> *mut u8 {
unsafe {
libc::realloc(ptr as *mut _, size as libc::size_t) as *mut u8
libc::realloc(ptr as *mut _, size) as *mut u8
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/bench/shootout-reverse-complement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ impl Tables {

/// Finds the first position at which `b` occurs in `s`.
fn memchr(h: &[u8], n: u8) -> Option<usize> {
use libc::{c_void, c_int, size_t};
use libc::{c_void, c_int};
let res = unsafe {
libc::memchr(h.as_ptr() as *const c_void, n as c_int, h.len() as size_t)
libc::memchr(h.as_ptr() as *const c_void, n as c_int, h.len())
};
if res.is_null() {
None
Expand Down
3 changes: 1 addition & 2 deletions src/test/run-pass/regions-mock-trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ struct Ccx {

fn alloc<'a>(_bcx : &'a arena) -> &'a Bcx<'a> {
unsafe {
mem::transmute(libc::malloc(mem::size_of::<Bcx<'a>>()
as libc::size_t))
mem::transmute(libc::malloc(mem::size_of::<Bcx<'a>>()))
}
}

Expand Down