Skip to content

Commit 5c3fe11

Browse files
committed
std: Avoid use of libc in portable modules
This commit removes usage of the `libc` crate in "portable" modules like those at the top level and `sys_common`. Instead common types like `*mut u8` or `u32` are used instead of `*mut c_void` or `c_int` as well as switching to platform-specific functions like `sys::strlen` instead of `libc::strlen`.
1 parent 348930e commit 5c3fe11

File tree

16 files changed

+33
-31
lines changed

16 files changed

+33
-31
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ use cmp::Ordering;
1414
use error::Error;
1515
use fmt::{self, Write};
1616
use io;
17-
use libc;
1817
use mem;
1918
use memchr;
2019
use ops;
2120
use os::raw::c_char;
2221
use ptr;
2322
use slice;
2423
use str::{self, Utf8Error};
24+
use sys;
2525

2626
/// A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the
2727
/// middle.
@@ -404,7 +404,7 @@ impl CString {
404404
/// ```
405405
#[stable(feature = "cstr_memory", since = "1.4.0")]
406406
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
407-
let len = libc::strlen(ptr) + 1; // Including the NUL byte
407+
let len = sys::strlen(ptr) + 1; // Including the NUL byte
408408
let slice = slice::from_raw_parts_mut(ptr, len as usize);
409409
CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
410410
}
@@ -861,7 +861,7 @@ impl CStr {
861861
/// ```
862862
#[stable(feature = "rust1", since = "1.0.0")]
863863
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
864-
let len = libc::strlen(ptr);
864+
let len = sys::strlen(ptr);
865865
let ptr = ptr as *const u8;
866866
CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len as usize + 1))
867867
}

src/libstd/sys/redox/backtrace/tracing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
9696

9797
if cx.idx < cx.frames.len() {
9898
cx.frames[cx.idx] = Frame {
99-
symbol_addr: symaddr,
100-
exact_position: ip,
99+
symbol_addr: symaddr as *mut u8,
100+
exact_position: ip as *mut u8,
101101
};
102102
cx.idx += 1;
103103
}

src/libstd/sys/redox/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use io::{self, ErrorKind};
1414

15+
pub use libc::strlen;
1516
pub use self::rand::hashmap_random_keys;
1617

1718
pub mod args;

src/libstd/sys/unix/backtrace/printing/dladdr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn resolve_symname<F>(frame: Frame,
2222
{
2323
unsafe {
2424
let mut info: Dl_info = intrinsics::init();
25-
let symname = if dladdr(frame.exact_position, &mut info) == 0 ||
25+
let symname = if dladdr(frame.exact_position as *mut _, &mut info) == 0 ||
2626
info.dli_sname.is_null() {
2727
None
2828
} else {
@@ -41,6 +41,5 @@ struct Dl_info {
4141
}
4242

4343
extern {
44-
fn dladdr(addr: *const libc::c_void,
45-
info: *mut Dl_info) -> libc::c_int;
44+
fn dladdr(addr: *const libc::c_void, info: *mut Dl_info) -> libc::c_int;
4645
}

src/libstd/sys/unix/backtrace/printing/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use self::dladdr::resolve_symname;
2020
#[cfg(target_os = "emscripten")]
2121
pub fn foreach_symbol_fileline<F>(_: Frame, _: F, _: &BacktraceContext) -> io::Result<bool>
2222
where
23-
F: FnMut(&[u8], ::libc::c_int) -> io::Result<()>
23+
F: FnMut(&[u8], u32) -> io::Result<()>
2424
{
2525
Ok(false)
2626
}

src/libstd/sys/unix/backtrace/tracing/backtrace_fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
3636
} as usize;
3737
for (from, to) in raw_frames.iter().zip(frames.iter_mut()).take(nb_frames) {
3838
*to = Frame {
39-
exact_position: *from,
40-
symbol_addr: *from,
39+
exact_position: *from as *mut u8,
40+
symbol_addr: *from as *mut u8,
4141
};
4242
}
4343
Ok((nb_frames as usize, BacktraceContext))

src/libstd/sys/unix/backtrace/tracing/gcc_s.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
9696

9797
if cx.idx < cx.frames.len() {
9898
cx.frames[cx.idx] = Frame {
99-
symbol_addr: symaddr,
100-
exact_position: ip,
99+
symbol_addr: symaddr as *mut u8,
100+
exact_position: ip as *mut u8,
101101
};
102102
cx.idx += 1;
103103
}

src/libstd/sys/unix/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use libc;
3030
#[cfg(all(not(dox), target_os = "l4re"))] pub use os::linux as platform;
3131

3232
pub use self::rand::hashmap_random_keys;
33+
pub use libc::strlen;
3334

3435
#[macro_use]
3536
pub mod weak;

src/libstd/sys/unix/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Thread {
8787
};
8888

8989
extern fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
90-
unsafe { start_thread(main); }
90+
unsafe { start_thread(main as *mut u8); }
9191
ptr::null_mut()
9292
}
9393
}

src/libstd/sys/windows/backtrace/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
9595
frame.AddrReturn.Offset == 0 { break }
9696

9797
frames[i] = Frame {
98-
symbol_addr: (addr - 1) as *const c_void,
99-
exact_position: (addr - 1) as *const c_void,
98+
symbol_addr: (addr - 1) as *const u8,
99+
exact_position: (addr - 1) as *const u8,
100100
};
101101
i += 1;
102102
}

0 commit comments

Comments
 (0)