Skip to content

Commit b5550b2

Browse files
committed
capstone-rs: Remove runtime depdendency on libc
* C types under `core::ffi` have been stable since 1.64.0 * `core::ffi::CStr` was already in use, so we can use it instead of `libc::strlen`. Since libcore has everything we need, remove the dependency on the libc crate at runtime.
1 parent 2b881e8 commit b5550b2

File tree

8 files changed

+13
-19
lines changed

8 files changed

+13
-19
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

capstone-rs/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ travis-ci = { repository = "capstone-rust/capstone-rs" }
1717

1818
[dependencies]
1919
capstone-sys = { path = "../capstone-sys", version = "0.17.0", default-features = false }
20-
libc = { version = "0.2", default-features = false }
2120
static_assertions = "1.1.0"
2221

2322
[dev-dependencies]

capstone-rs/src/arch/arm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
use core::convert::{From, TryInto};
44
use core::{cmp, fmt, slice};
5+
use core::ffi::c_uint;
56

67
use capstone_sys::{
78
arm_op_mem, arm_op_type, arm_shifter, cs_ac_type, cs_arm, cs_arm_op, cs_arm_op__bindgen_ty_2};
8-
use libc::c_uint;
99

1010
pub use crate::arch::arch_builder::arm::*;
1111
use crate::arch::DetailsArchInsn;
@@ -301,7 +301,7 @@ mod test {
301301
fn test_armshift() {
302302
use super::arm_shifter::*;
303303
use super::ArmShift::*;
304-
use libc::c_uint;
304+
use core::ffi::c_uint;
305305

306306
fn t(shift_type_value: (arm_shifter, c_uint), arm_shift: ArmShift) {
307307
let (shift_type, value) = shift_type_value;

capstone-rs/src/arch/arm64.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! Contains arm64-specific types
22
3-
use libc::c_uint;
4-
53
pub use crate::arch::arch_builder::arm64::*;
64
use crate::arch::DetailsArchInsn;
75
use crate::instruction::{AccessType, RegId, RegIdInt};
86
use capstone_sys::{arm64_op_mem, arm64_op_sme_index, arm64_op_type, cs_ac_type, cs_arm64, cs_arm64_op};
97
use core::convert::{From, TryInto};
108
use core::{cmp, fmt, mem, slice};
9+
use core::ffi::c_uint;
1110

1211
// Re-exports
1312
pub use capstone_sys::arm64_barrier_op as ArmBarrierOp;
@@ -306,7 +305,7 @@ mod test {
306305
fn test_arm64shift() {
307306
use super::arm64_shifter::*;
308307
use super::Arm64Shift::*;
309-
use libc::c_uint;
308+
use core::ffi::c_uint;
310309

311310
fn t(shift_type_value: (arm64_shifter, c_uint), arm64_shift: Arm64Shift) {
312311
let (shift_type, value) = shift_type_value;

capstone-rs/src/arch/tms320c64x.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use core::convert::From;
44
use core::{cmp, fmt, slice};
5+
use core::ffi::c_int;
56

6-
use libc::c_int;
77
use capstone_sys::{
88
cs_tms320c64x, cs_tms320c64x_op, tms320c64x_funit, tms320c64x_mem_dir, tms320c64x_mem_disp,
99
tms320c64x_mem_mod, tms320c64x_op_mem, tms320c64x_op_type,
@@ -256,7 +256,7 @@ def_arch_details_struct!(
256256
mod test {
257257
use super::*;
258258
use capstone_sys::*;
259-
use libc::{c_int, c_uint};
259+
use core::ffi::{c_int, c_uint};
260260

261261
const OP_MEM_ZERO: tms320c64x_op_mem = tms320c64x_op_mem {
262262
base: 0,

capstone-rs/src/capstone.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use alloc::boxed::Box;
22
use alloc::string::String;
33
use core::convert::From;
4-
use core::ffi::CStr;
4+
use core::ffi::{c_int, c_uint, c_void, CStr};
55
use core::marker::PhantomData;
66
use core::mem::MaybeUninit;
77
#[cfg(feature = "std")]
88
use std::ffi::CString;
99

10-
use libc::{c_int, c_void};
11-
1210
use capstone_sys::cs_opt_value::*;
1311
use capstone_sys::*;
1412

@@ -17,7 +15,7 @@ use crate::constants::{Arch, Endian, ExtraMode, Mode, OptValue, Syntax};
1715
use crate::instruction::{Insn, InsnDetail, InsnGroupId, InsnId, Instructions, RegId};
1816
use crate::{error::*, PartialInitRegsAccess};
1917

20-
use {crate::ffi::str_from_cstr_ptr, alloc::string::ToString, libc::c_uint};
18+
use {crate::ffi::str_from_cstr_ptr, alloc::string::ToString};
2119

2220
/// Length of `cs_regs`
2321
pub(crate) const REGS_ACCESS_BUF_LEN: usize = 64;

capstone-rs/src/ffi.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Functions useful for FFI
22
3-
use core::{slice, str};
4-
use libc::{self, c_char};
3+
use core::ffi::c_char;
4+
use core::ffi::CStr;
55

66
// This function will go unused in Diet mode
77
#[allow(unused)]
@@ -17,11 +17,10 @@ pub(crate) unsafe fn str_from_cstr_ptr<'a>(ptr: *const c_char) -> Option<&'a str
1717
if ptr.is_null() {
1818
return None;
1919
}
20-
let len = libc::strlen(ptr);
2120
/* ASSUMPTION: capstone returns NUL terminated string */
22-
let view: &[u8] = slice::from_raw_parts(ptr as *const u8, len as usize);
21+
let cstr = CStr::from_ptr(ptr);
2322
/* ASSUMPTION: capstone returns a valid UTF-8 string */
24-
Some(str::from_utf8_unchecked(view))
23+
Some(core::str::from_utf8_unchecked(cstr.to_bytes()))
2524
}
2625

2726
#[cfg(test)]

capstone-rs/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
clippy::upper_case_acronyms
66
)]
77

8+
use core::ffi::c_uint;
89
use core::{convert::TryInto, fmt::Debug, mem::MaybeUninit};
910

1011
use alloc::vec::Vec;
1112
#[cfg(feature = "full")]
1213
use {alloc::string::String, std::collections::HashSet};
1314

1415
use capstone_sys::cs_group_type;
15-
use libc::c_uint;
1616
use pretty_assertions::assert_eq;
1717

1818
use super::arch::*;

0 commit comments

Comments
 (0)