Skip to content

Commit 6f39686

Browse files
authored
refactor: enable lint unsafe_op_in_unsafe_fn (#2180)
1 parent 6bacfe0 commit 6f39686

File tree

18 files changed

+308
-228
lines changed

18 files changed

+308
-228
lines changed

src/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub unsafe fn clearenv() -> std::result::Result<(), ClearEnvError> {
4646
target_os = "linux",
4747
target_os = "android",
4848
target_os = "emscripten"))] {
49-
let ret = libc::clearenv();
49+
let ret = unsafe { libc::clearenv() };
5050
} else {
5151
use std::env;
5252
for (name, _) in env::vars_os() {

src/errno.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ cfg_if! {
1010
if #[cfg(any(target_os = "freebsd",
1111
apple_targets,))] {
1212
unsafe fn errno_location() -> *mut c_int {
13-
libc::__error()
13+
unsafe { libc::__error() }
1414
}
1515
} else if #[cfg(any(target_os = "android",
1616
target_os = "netbsd",
1717
target_os = "openbsd"))] {
1818
unsafe fn errno_location() -> *mut c_int {
19-
libc::__errno()
19+
unsafe { libc::__errno() }
2020
}
2121
} else if #[cfg(any(target_os = "linux",
2222
target_os = "redox",
2323
target_os = "dragonfly",
2424
target_os = "fuchsia"))] {
2525
unsafe fn errno_location() -> *mut c_int {
26-
libc::__errno_location()
26+
unsafe { libc::__errno_location() }
2727
}
2828
} else if #[cfg(any(target_os = "illumos", target_os = "solaris"))] {
2929
unsafe fn errno_location() -> *mut c_int {
30-
libc::___errno()
30+
unsafe { libc::___errno() }
3131
}
3232
} else if #[cfg(any(target_os = "haiku",))] {
3333
unsafe fn errno_location() -> *mut c_int {
34-
libc::_errnop()
34+
unsafe { libc::_errnop() }
3535
}
3636
} else if #[cfg(any(target_os = "aix"))] {
3737
unsafe fn errno_location() -> *mut c_int {
38-
libc::_Errno()
38+
unsafe { libc::_Errno() }
3939
}
4040
}
4141
}

src/ifaddrs.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,24 @@ unsafe fn workaround_xnu_bug(info: &libc::ifaddrs) -> Option<SockaddrStorage> {
6262

6363
let mut dst_sock = mem::MaybeUninit::<libc::sockaddr_storage>::zeroed();
6464

65-
// memcpy only sa_len bytes, assume the rest is zero
66-
std::ptr::copy_nonoverlapping(
67-
src_sock as *const u8,
68-
dst_sock.as_mut_ptr().cast(),
69-
(*src_sock).sa_len.into(),
70-
);
71-
72-
// Initialize ss_len to sizeof(libc::sockaddr_storage).
73-
(*dst_sock.as_mut_ptr()).ss_len =
74-
u8::try_from(mem::size_of::<libc::sockaddr_storage>()).unwrap();
75-
let dst_sock = dst_sock.assume_init();
65+
let dst_sock = unsafe {
66+
// memcpy only sa_len bytes, assume the rest is zero
67+
std::ptr::copy_nonoverlapping(
68+
src_sock as *const u8,
69+
dst_sock.as_mut_ptr().cast(),
70+
(*src_sock).sa_len.into(),
71+
);
72+
73+
// Initialize ss_len to sizeof(libc::sockaddr_storage).
74+
(*dst_sock.as_mut_ptr()).ss_len =
75+
u8::try_from(mem::size_of::<libc::sockaddr_storage>()).unwrap();
76+
dst_sock.assume_init()
77+
};
7678

7779
let dst_sock_ptr =
7880
&dst_sock as *const libc::sockaddr_storage as *const libc::sockaddr;
7981

80-
SockaddrStorage::from_raw(dst_sock_ptr, None)
82+
unsafe { SockaddrStorage::from_raw(dst_sock_ptr, None) }
8183
}
8284

8385
impl InterfaceAddress {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#![warn(missing_docs)]
9090
#![cfg_attr(docsrs, feature(doc_cfg))]
9191
#![deny(clippy::cast_ptr_alignment)]
92+
#![deny(unsafe_op_in_unsafe_fn)]
9293

9394
// Re-exported external crates
9495
pub use libc;

src/pty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ pub fn posix_openpt(flags: fcntl::OFlag) -> Result<PtyMaster> {
169169
/// For a threadsafe and non-`unsafe` alternative on Linux, see `ptsname_r()`.
170170
#[inline]
171171
pub unsafe fn ptsname(fd: &PtyMaster) -> Result<String> {
172-
let name_ptr = libc::ptsname(fd.as_raw_fd());
172+
let name_ptr = unsafe { libc::ptsname(fd.as_raw_fd()) };
173173
if name_ptr.is_null() {
174174
return Err(Errno::last());
175175
}
176176

177-
let name = CStr::from_ptr(name_ptr);
177+
let name = unsafe { CStr::from_ptr(name_ptr) };
178178
Ok(name.to_string_lossy().into_owned())
179179
}
180180

@@ -341,15 +341,15 @@ pub unsafe fn forkpty<'a, 'b, T: Into<Option<&'a Winsize>>, U: Into<Option<&'b T
341341
.map(|ws| ws as *const Winsize as *mut _)
342342
.unwrap_or(ptr::null_mut());
343343

344-
let res = libc::forkpty(master.as_mut_ptr(), ptr::null_mut(), term, win);
344+
let res = unsafe { libc::forkpty(master.as_mut_ptr(), ptr::null_mut(), term, win) };
345345

346346
let fork_result = Errno::result(res).map(|res| match res {
347347
0 => ForkResult::Child,
348348
res => ForkResult::Parent { child: Pid::from_raw(res) },
349349
})?;
350350

351351
Ok(ForkptyResult {
352-
master: OwnedFd::from_raw_fd(master.assume_init()),
352+
master: unsafe { OwnedFd::from_raw_fd( master.assume_init() ) },
353353
fork_result,
354354
})
355355
}

src/sched.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,19 @@ mod sched_linux_like {
116116
}
117117

118118
let combined = flags.bits() | signal.unwrap_or(0);
119-
let ptr = stack.as_mut_ptr().add(stack.len());
120-
let ptr_aligned = ptr.sub(ptr as usize % 16);
121-
let res = libc::clone(
122-
mem::transmute(
123-
callback
124-
as extern "C" fn(*mut Box<dyn FnMut() -> isize>) -> i32,
125-
),
126-
ptr_aligned as *mut c_void,
127-
combined,
128-
&mut cb as *mut _ as *mut c_void,
129-
);
119+
let res = unsafe {
120+
let ptr = stack.as_mut_ptr().add(stack.len());
121+
let ptr_aligned = ptr.sub(ptr as usize % 16);
122+
libc::clone(
123+
mem::transmute(
124+
callback
125+
as extern "C" fn(*mut Box<dyn FnMut() -> isize>) -> i32,
126+
),
127+
ptr_aligned as *mut c_void,
128+
combined,
129+
&mut cb as *mut _ as *mut c_void,
130+
)
131+
};
130132

131133
Errno::result(res).map(Pid::from_raw)
132134
}

src/sys/inotify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl Inotify {
244244
impl FromRawFd for Inotify {
245245
unsafe fn from_raw_fd(fd: RawFd) -> Self {
246246
Inotify {
247-
fd: OwnedFd::from_raw_fd(fd),
247+
fd: unsafe { OwnedFd::from_raw_fd(fd) },
248248
}
249249
}
250250
}

src/sys/ioctl/mod.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
//! # const SPI_IOC_MAGIC: u8 = b'k'; // Defined in linux/spi/spidev.h
7373
//! # const SPI_IOC_TYPE_MODE: u8 = 1;
7474
//! pub unsafe fn spi_read_mode(fd: c_int, data: *mut u8) -> Result<c_int> {
75-
//! let res = libc::ioctl(fd, request_code_read!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MODE, mem::size_of::<u8>()), data);
75+
//! let res = unsafe { libc::ioctl(fd, request_code_read!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MODE, mem::size_of::<u8>()), data) };
7676
//! Errno::result(res)
7777
//! }
7878
//! # fn main() {}
@@ -179,9 +179,13 @@
179179
//! # const SPI_IOC_TYPE_MESSAGE: u8 = 0;
180180
//! # pub struct spi_ioc_transfer(u64);
181181
//! pub unsafe fn spi_message(fd: c_int, data: &mut [spi_ioc_transfer]) -> Result<c_int> {
182-
//! let res = libc::ioctl(fd,
183-
//! request_code_write!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MESSAGE, data.len() * mem::size_of::<spi_ioc_transfer>()),
184-
//! data.as_ptr());
182+
//! let res = unsafe {
183+
//! libc::ioctl(
184+
//! fd,
185+
//! request_code_write!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MESSAGE, data.len() * mem::size_of::<spi_ioc_transfer>()),
186+
//! data
187+
//! )
188+
//! };
185189
//! Errno::result(res)
186190
//! }
187191
//! # fn main() {}
@@ -303,7 +307,9 @@ macro_rules! ioctl_none {
303307
$(#[$attr])*
304308
pub unsafe fn $name(fd: $crate::libc::c_int)
305309
-> $crate::Result<$crate::libc::c_int> {
306-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_none!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type))
310+
unsafe {
311+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_none!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type))
312+
}
307313
}
308314
)
309315
}
@@ -343,7 +349,9 @@ macro_rules! ioctl_none_bad {
343349
$(#[$attr])*
344350
pub unsafe fn $name(fd: $crate::libc::c_int)
345351
-> $crate::Result<$crate::libc::c_int> {
346-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
352+
unsafe {
353+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
354+
}
347355
}
348356
)
349357
}
@@ -381,7 +389,9 @@ macro_rules! ioctl_read {
381389
pub unsafe fn $name(fd: $crate::libc::c_int,
382390
data: *mut $ty)
383391
-> $crate::Result<$crate::libc::c_int> {
384-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
392+
unsafe {
393+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
394+
}
385395
}
386396
)
387397
}
@@ -417,7 +427,9 @@ macro_rules! ioctl_read_bad {
417427
pub unsafe fn $name(fd: $crate::libc::c_int,
418428
data: *mut $ty)
419429
-> $crate::Result<$crate::libc::c_int> {
420-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
430+
unsafe {
431+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
432+
}
421433
}
422434
)
423435
}
@@ -454,7 +466,9 @@ macro_rules! ioctl_write_ptr {
454466
pub unsafe fn $name(fd: $crate::libc::c_int,
455467
data: *const $ty)
456468
-> $crate::Result<$crate::libc::c_int> {
457-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
469+
unsafe {
470+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
471+
}
458472
}
459473
)
460474
}
@@ -490,7 +504,9 @@ macro_rules! ioctl_write_ptr_bad {
490504
pub unsafe fn $name(fd: $crate::libc::c_int,
491505
data: *const $ty)
492506
-> $crate::Result<$crate::libc::c_int> {
493-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
507+
unsafe {
508+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
509+
}
494510
}
495511
)
496512
}
@@ -531,7 +547,9 @@ cfg_if! {
531547
pub unsafe fn $name(fd: $crate::libc::c_int,
532548
data: $crate::sys::ioctl::ioctl_param_type)
533549
-> $crate::Result<$crate::libc::c_int> {
534-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write_int!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type, data))
550+
unsafe {
551+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write_int!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type, data))
552+
}
535553
}
536554
)
537555
}
@@ -572,7 +590,9 @@ cfg_if! {
572590
pub unsafe fn $name(fd: $crate::libc::c_int,
573591
data: $crate::sys::ioctl::ioctl_param_type)
574592
-> $crate::Result<$crate::libc::c_int> {
575-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$crate::libc::c_int>()) as $crate::sys::ioctl::ioctl_num_type, data))
593+
unsafe {
594+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$crate::libc::c_int>()) as $crate::sys::ioctl::ioctl_num_type, data))
595+
}
576596
}
577597
)
578598
}
@@ -616,7 +636,9 @@ macro_rules! ioctl_write_int_bad {
616636
pub unsafe fn $name(fd: $crate::libc::c_int,
617637
data: $crate::libc::c_int)
618638
-> $crate::Result<$crate::libc::c_int> {
619-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
639+
unsafe {
640+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
641+
}
620642
}
621643
)
622644
}
@@ -653,7 +675,9 @@ macro_rules! ioctl_readwrite {
653675
pub unsafe fn $name(fd: $crate::libc::c_int,
654676
data: *mut $ty)
655677
-> $crate::Result<$crate::libc::c_int> {
656-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
678+
unsafe {
679+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
680+
}
657681
}
658682
)
659683
}
@@ -681,7 +705,9 @@ macro_rules! ioctl_readwrite_bad {
681705
pub unsafe fn $name(fd: $crate::libc::c_int,
682706
data: *mut $ty)
683707
-> $crate::Result<$crate::libc::c_int> {
684-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
708+
unsafe {
709+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
710+
}
685711
}
686712
)
687713
}
@@ -710,7 +736,9 @@ macro_rules! ioctl_read_buf {
710736
pub unsafe fn $name(fd: $crate::libc::c_int,
711737
data: &mut [$ty])
712738
-> $crate::Result<$crate::libc::c_int> {
713-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
739+
unsafe {
740+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
741+
}
714742
}
715743
)
716744
}
@@ -749,7 +777,9 @@ macro_rules! ioctl_write_buf {
749777
pub unsafe fn $name(fd: $crate::libc::c_int,
750778
data: &[$ty])
751779
-> $crate::Result<$crate::libc::c_int> {
752-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_ptr()))
780+
unsafe {
781+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_ptr()))
782+
}
753783
}
754784
)
755785
}
@@ -778,7 +808,9 @@ macro_rules! ioctl_readwrite_buf {
778808
pub unsafe fn $name(fd: $crate::libc::c_int,
779809
data: &mut [$ty])
780810
-> $crate::Result<$crate::libc::c_int> {
781-
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
811+
unsafe {
812+
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
813+
}
782814
}
783815
)
784816
}

0 commit comments

Comments
 (0)