mirror of
https://github.com/rui314/mold.git
synced 2026-11-09 04:49:33 -10:00
Use pointer casts and raw borrows instead of as casts
Apply clippy's ptr_as_ptr, borrow_as_ptr, ref_as_ptr and ptr_cast_constness suggestions. `ptr.cast()` and `ptr::from_ref()` cannot silently change the pointee type when a declaration elsewhere changes, and `&raw mut x` passes a pointer to C without creating an intermediate reference.
This commit is contained in:
1 parent
b56daa27b7
commit
c5f69d9181
9 files changed
+25
-23
No files matched your search
+2
-2
@@ -3639,14 +3639,14 @@ mod tests {
|
||||
for _ in 0..17 {
|
||||
files.push(Box::new(File { index: 0, value: 0 }));
|
||||
}
|
||||
let first = &files[0] as *const File;
|
||||
let first = &raw const files[0];
|
||||
files.retain(|file| file.index % 2 == 0);
|
||||
files.par_iter_mut().enumerate().for_each(|(i, file)| {
|
||||
assert_eq!(file.index, i as u32 * 2);
|
||||
file.value = i as u32 + 1;
|
||||
});
|
||||
assert_eq!(files.pool_len(), 17);
|
||||
assert_eq!(&files[0] as *const File, first);
|
||||
assert_eq!(&raw const files[0], first);
|
||||
for (i, file) in files.pool_iter().enumerate() {
|
||||
assert_eq!(file.value, if i % 2 == 0 { i as u32 / 2 + 1 } else { 0 });
|
||||
}
|
||||
|
||||
@@ -832,7 +832,8 @@ impl<E: Arch> InputSection<E> {
|
||||
|
||||
let isec = sym.input_section_ref(ctx);
|
||||
let discarded = sym.file().is_none() && sym.name().is_empty() && !sym.is_fragment_dummy();
|
||||
let discarded = discarded && std::ptr::eq(sym, &ctx.symbols[SymbolId::DISCARDED_COMDAT]);
|
||||
let discarded =
|
||||
discarded && std::ptr::eq(sym, &raw const ctx.symbols[SymbolId::DISCARDED_COMDAT]);
|
||||
|
||||
// Setting a tombstone is a special feature for a dead debug section.
|
||||
match isec {
|
||||
|
||||
+8
-8
@@ -330,7 +330,7 @@ unsafe extern "C" fn add_symbols(
|
||||
|
||||
/// Receives an object file the plugin compiled.
|
||||
unsafe extern "C" fn add_input_file<E: Arch>(path: *const c_char) -> c_int {
|
||||
let ctx = unsafe { &mut *(CONTEXT.load(Ordering::Acquire) as *mut Context<E>) };
|
||||
let ctx = unsafe { &mut *CONTEXT.load(Ordering::Acquire).cast::<Context<E>>() };
|
||||
let path = crate::util::os_str(unsafe { CStr::from_ptr(path) }.to_bytes());
|
||||
let mf = must_open_file(std::path::Path::new(""), path);
|
||||
mf.set_dependency(false);
|
||||
@@ -368,8 +368,8 @@ unsafe extern "C" fn set_extra_library_path(_path: *const c_char) -> c_int {
|
||||
}
|
||||
|
||||
unsafe extern "C" fn get_view(handle: *const c_void, view: *mut *const c_void) -> c_int {
|
||||
let mf = unsafe { &*(handle as *const MappedFile) };
|
||||
unsafe { *view = mf.data().as_ptr() as *const c_void };
|
||||
let mf = unsafe { &*handle.cast::<MappedFile>() };
|
||||
unsafe { *view = mf.data().as_ptr().cast() };
|
||||
LDPS_OK
|
||||
}
|
||||
|
||||
@@ -477,7 +477,7 @@ unsafe fn get_symbols<E: Arch>(
|
||||
) -> c_int {
|
||||
let ctx = unsafe { &*(CONTEXT.load(Ordering::Acquire) as *const Context<E>) };
|
||||
let psyms = unsafe { std::slice::from_raw_parts_mut(psyms, nsyms as usize) };
|
||||
let handle = handle as *const MappedFile;
|
||||
let handle = handle.cast::<MappedFile>();
|
||||
let Some(file) = ctx.objs.iter().find(|f| f.base.mf.is_some_and(|mf| ptr::eq(mf, handle)))
|
||||
else {
|
||||
return LDPS_BAD_HANDLE;
|
||||
@@ -717,7 +717,7 @@ fn plugin_input_file(mf: &'static MappedFile) -> (PluginInputFile, File) {
|
||||
fd: file.as_raw_handle(),
|
||||
offset: mf.offset() as u64,
|
||||
filesize: mf.size() as u64,
|
||||
handle: mf as *const MappedFile as *mut c_void,
|
||||
handle: std::ptr::from_ref(mf) as *mut c_void,
|
||||
};
|
||||
(input, file)
|
||||
}
|
||||
@@ -747,7 +747,7 @@ pub fn read_lto_object<E: Arch>(
|
||||
let mut claimed: c_int = 0;
|
||||
// claim_file_hook() calls add_symbols() which initializes `plugin_symbols`
|
||||
// SAFETY: `input` describes an open file.
|
||||
unsafe { claim_file(&input, &mut claimed) };
|
||||
unsafe { claim_file(&raw const input, &raw mut claimed) };
|
||||
drop(file);
|
||||
|
||||
if claimed == 0 {
|
||||
@@ -883,7 +883,7 @@ pub fn run_plugin<E: Arch>(ctx: &mut Context<E>) {
|
||||
let (input, _file) = plugin_input_file(file.base.mf.unwrap());
|
||||
let mut claimed: c_int = 0;
|
||||
// SAFETY: `input` describes an open file.
|
||||
unsafe { claim_file(&input, &mut claimed) };
|
||||
unsafe { claim_file(&raw const input, &raw mut claimed) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -893,7 +893,7 @@ pub fn run_plugin<E: Arch>(ctx: &mut Context<E>) {
|
||||
.unwrap()
|
||||
.all_symbols_read
|
||||
.expect("the plugin registered an all_symbols_read hook");
|
||||
CONTEXT.store(ctx as *mut Context<E> as *mut c_void, Ordering::Release);
|
||||
CONTEXT.store(std::ptr::from_mut(ctx).cast(), Ordering::Release);
|
||||
// SAFETY: the callbacks are the only users of the context until the
|
||||
// hook returns.
|
||||
let status = unsafe { all_symbols_read() };
|
||||
|
||||
+2
-1
@@ -140,7 +140,8 @@ fn preallocate(file: &File, offset: u64, size: u64) {
|
||||
// descriptor; the statfs buffer is fully written before it is read.
|
||||
unsafe {
|
||||
let mut fs: libc::statfs = std::mem::zeroed();
|
||||
if libc::fstatfs(file.as_raw_fd(), &mut fs) != 0 || fs.f_type != libc::TMPFS_MAGIC as _ {
|
||||
if libc::fstatfs(file.as_raw_fd(), &raw mut fs) != 0 || fs.f_type != libc::TMPFS_MAGIC as _
|
||||
{
|
||||
libc::fallocate(file.as_raw_fd(), 0, offset as libc::off_t, size as libc::off_t);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4049,7 +4049,7 @@ pub fn write_build_id<E: Arch>(ctx: &mut Context<E>, buf: &mut [u8], is_mmapped:
|
||||
// that are backed by the file.
|
||||
unsafe {
|
||||
libc::madvise(
|
||||
shard.as_mut_ptr() as *mut libc::c_void,
|
||||
shard.as_mut_ptr().cast(),
|
||||
shard.len(),
|
||||
libc::MADV_DONTNEED,
|
||||
)
|
||||
|
||||
+7
-7
@@ -39,11 +39,11 @@ pub fn fork_child() {
|
||||
// Parent
|
||||
drop(writer);
|
||||
let mut buf = [0u8; 1];
|
||||
if libc::read(reader.as_raw_fd(), buf.as_mut_ptr() as *mut libc::c_void, 1) == 1 {
|
||||
if libc::read(reader.as_raw_fd(), buf.as_mut_ptr().cast(), 1) == 1 {
|
||||
libc::_exit(0);
|
||||
}
|
||||
let mut status = 0;
|
||||
libc::waitpid(pid, &mut status, 0);
|
||||
libc::waitpid(pid, &raw mut status, 0);
|
||||
if libc::WIFEXITED(status) {
|
||||
libc::_exit(libc::WEXITSTATUS(status));
|
||||
}
|
||||
@@ -70,7 +70,7 @@ pub fn notify_parent() {
|
||||
let buf = [1u8];
|
||||
// SAFETY: writer owns a valid pipe write end.
|
||||
unsafe {
|
||||
libc::write(writer.as_raw_fd(), buf.as_ptr() as *const libc::c_void, 1);
|
||||
libc::write(writer.as_raw_fd(), buf.as_ptr().cast(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ extern "C" fn on_signal(
|
||||
let msg = b"mold: failed to write to an output file. Disk full?\n";
|
||||
// SAFETY: write is async-signal-safe.
|
||||
unsafe {
|
||||
libc::write(libc::STDERR_FILENO, msg.as_ptr() as *const libc::c_void, msg.len());
|
||||
libc::write(libc::STDERR_FILENO, msg.as_ptr().cast(), msg.len());
|
||||
}
|
||||
}
|
||||
crate::output_file::cleanup();
|
||||
@@ -129,10 +129,10 @@ pub fn install_signal_handler() {
|
||||
unsafe {
|
||||
let mut action: libc::sigaction = std::mem::zeroed();
|
||||
action.sa_sigaction = on_signal as *const () as libc::sighandler_t;
|
||||
libc::sigemptyset(&mut action.sa_mask);
|
||||
libc::sigemptyset(&raw mut action.sa_mask);
|
||||
action.sa_flags = libc::SA_SIGINFO;
|
||||
libc::sigaction(libc::SIGSEGV, &action, std::ptr::null_mut());
|
||||
libc::sigaction(libc::SIGBUS, &action, std::ptr::null_mut());
|
||||
libc::sigaction(libc::SIGSEGV, &raw const action, std::ptr::null_mut());
|
||||
libc::sigaction(libc::SIGBUS, &raw const action, std::ptr::null_mut());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ fn zlib_compress(input: &[u8], level: u32) -> Vec<u8> {
|
||||
// https://github.com/ianlancetaylor/libbacktrace/pull/87
|
||||
let mut nbits = 0;
|
||||
// SAFETY: stream is initialized and nbits is a valid output pointer.
|
||||
let status = unsafe { deflate_pending(stream, std::ptr::null_mut(), &mut nbits) };
|
||||
let status = unsafe { deflate_pending(stream, std::ptr::null_mut(), &raw mut nbits) };
|
||||
assert_eq!(status, libz_sys::Z_OK);
|
||||
if nbits == 5 {
|
||||
// SAFETY: stream is initialized and has enough pending-buffer space.
|
||||
|
||||
@@ -280,7 +280,7 @@ impl<T> ConcurrentMap<T> {
|
||||
(*ent.value.get()).write(value);
|
||||
*ent.keylen.get() = keylen;
|
||||
}
|
||||
ent.key.store(key as *mut u8, Ordering::Release);
|
||||
ent.key.store(key.cast_mut(), Ordering::Release);
|
||||
return (EntryId(idx as u32), self.value_at(idx), true);
|
||||
}
|
||||
Err(current) => ptr = current,
|
||||
|
||||
+1
-1
@@ -134,7 +134,7 @@ unsafe extern "system" {
|
||||
fn rusage() -> (f64, f64) {
|
||||
// SAFETY: `usage` is a valid, writable rusage struct.
|
||||
let mut usage: libc::rusage = unsafe { std::mem::zeroed() };
|
||||
unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut usage) };
|
||||
unsafe { libc::getrusage(libc::RUSAGE_SELF, &raw mut usage) };
|
||||
let to_secs = |t: libc::timeval| t.tv_sec as f64 + t.tv_usec as f64 / 1_000_000.0;
|
||||
(to_secs(usage.ru_utime), to_secs(usage.ru_stime))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user