Skip to content

Commit adaaa3f

Browse files
authored
Merge pull request #9687 from xtqqczze/lint/as_ptr
clippy: fix ptr lints
2 parents 4aabacb + cf1f618 commit adaaa3f

File tree

9 files changed

+40
-46
lines changed

9 files changed

+40
-46
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,6 @@ should_panic_without_expect = "allow" # 2
667667
doc_markdown = "allow"
668668
unused_self = "allow"
669669
enum_glob_use = "allow"
670-
ptr_cast_constness = "allow"
671-
borrow_as_ptr = "allow"
672-
ptr_as_ptr = "allow"
673670
needless_raw_string_hashes = "allow"
674671
unreadable_literal = "allow"
675672
unnested_or_patterns = "allow"

fuzz/uufuzz/src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,8 @@ fn read_from_fd(fd: RawFd) -> String {
193193
let mut captured_output = Vec::new();
194194
let mut read_buffer = [0; 1024];
195195
loop {
196-
let bytes_read = unsafe {
197-
libc::read(
198-
fd,
199-
read_buffer.as_mut_ptr() as *mut libc::c_void,
200-
read_buffer.len(),
201-
)
202-
};
196+
let bytes_read =
197+
unsafe { libc::read(fd, read_buffer.as_mut_ptr().cast(), read_buffer.len()) };
203198

204199
if bytes_read == -1 {
205200
eprintln!("Failed to read from the pipe");

src/uu/chroot/src/chroot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ fn enter_chroot(root: &Path, skip_chdir: bool) -> UResult<()> {
439439
.map_err(|e| ChrootError::CannotEnter("root".to_string(), e.into()))?
440440
.as_bytes_with_nul()
441441
.as_ptr()
442-
.cast::<libc::c_char>(),
442+
.cast(),
443443
)
444444
};
445445

src/uucore/src/lib/features/entries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ macro_rules! f {
290290
unsafe {
291291
let data = $fid(k);
292292
if !data.is_null() {
293-
Ok($st::from_raw(ptr::read(data as *const _)))
293+
Ok($st::from_raw(ptr::read(data.cast_const())))
294294
} else {
295295
// FIXME: Resource limits, signals and I/O failure may
296296
// cause this too. See getpwnam(3).
@@ -317,12 +317,12 @@ macro_rules! f {
317317
// f!(getgrnam, getgrgid, gid_t, Group);
318318
let data = $fnam(cstring.as_ptr());
319319
if !data.is_null() {
320-
return Ok($st::from_raw(ptr::read(data as *const _)));
320+
return Ok($st::from_raw(ptr::read(data.cast_const())));
321321
}
322322
if let Ok(id) = k.parse::<$t>() {
323323
let data = $fid(id);
324324
if !data.is_null() {
325-
Ok($st::from_raw(ptr::read(data as *const _)))
325+
Ok($st::from_raw(ptr::read(data.cast_const())))
326326
} else {
327327
Err(IOError::new(
328328
ErrorKind::NotFound,

src/uucore/src/lib/features/fsext.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,19 @@ impl From<StatFs> for MountInfo {
347347
fn from(statfs: StatFs) -> Self {
348348
let dev_name = unsafe {
349349
// spell-checker:disable-next-line
350-
CStr::from_ptr(&statfs.f_mntfromname[0])
350+
CStr::from_ptr(statfs.f_mntfromname.as_ptr())
351351
.to_string_lossy()
352352
.into_owned()
353353
};
354354
let fs_type = unsafe {
355355
// spell-checker:disable-next-line
356-
CStr::from_ptr(&statfs.f_fstypename[0])
356+
CStr::from_ptr(statfs.f_fstypename.as_ptr())
357357
.to_string_lossy()
358358
.into_owned()
359359
};
360360
let mount_dir_bytes = unsafe {
361361
// spell-checker:disable-next-line
362-
CStr::from_ptr(&statfs.f_mntonname[0]).to_bytes()
362+
CStr::from_ptr(statfs.f_mntonname.as_ptr()).to_bytes()
363363
};
364364
let mount_dir = os_str_from_bytes(mount_dir_bytes).unwrap().into_owned();
365365

@@ -506,7 +506,7 @@ pub fn read_fs_list() -> UResult<Vec<MountInfo>> {
506506
))]
507507
{
508508
let mut mount_buffer_ptr: *mut StatFs = ptr::null_mut();
509-
let len = unsafe { get_mount_info(&mut mount_buffer_ptr, 1_i32) };
509+
let len = unsafe { get_mount_info(&raw mut mount_buffer_ptr, 1_i32) };
510510
if len < 0 {
511511
return Err(USimpleError::new(1, "get_mount_info() failed"));
512512
}
@@ -668,10 +668,10 @@ impl FsUsage {
668668
let path = to_nul_terminated_wide_string(path);
669669
GetDiskFreeSpaceW(
670670
path.as_ptr(),
671-
&mut sectors_per_cluster,
672-
&mut bytes_per_sector,
673-
&mut number_of_free_clusters,
674-
&mut total_number_of_clusters,
671+
&raw mut sectors_per_cluster,
672+
&raw mut bytes_per_sector,
673+
&raw mut number_of_free_clusters,
674+
&raw mut total_number_of_clusters,
675675
);
676676
}
677677

@@ -881,7 +881,7 @@ impl FsMeta for StatFs {
881881
fn fsid(&self) -> u64 {
882882
// Use type inference to determine the type of f_fsid
883883
// (libc::__fsid_t on Android, libc::fsid_t on other platforms)
884-
let f_fsid: &[u32; 2] = unsafe { &*(&raw const self.f_fsid as *const [u32; 2]) };
884+
let f_fsid: &[u32; 2] = unsafe { &*(&raw const self.f_fsid).cast() };
885885
((u64::from(f_fsid[0])) << 32) | u64::from(f_fsid[1])
886886
}
887887
#[cfg(not(any(
@@ -932,7 +932,7 @@ pub fn statfs(path: &OsStr) -> Result<StatFs, String> {
932932
Ok(p) => {
933933
let mut buffer: StatFs = unsafe { mem::zeroed() };
934934
unsafe {
935-
match statfs_fn(p.as_ptr(), &mut buffer) {
935+
match statfs_fn(p.as_ptr(), &raw mut buffer) {
936936
0 => Ok(buffer),
937937
_ => {
938938
let errno = IOError::last_os_error().raw_os_error().unwrap_or(0);

src/uucore/src/lib/features/systemd_logind.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod login {
5353
pub fn get_sessions() -> Result<Vec<String>, Box<dyn std::error::Error>> {
5454
let mut sessions_ptr: *mut *mut libc::c_char = ptr::null_mut();
5555

56-
let result = unsafe { ffi::sd_get_sessions(&mut sessions_ptr) };
56+
let result = unsafe { ffi::sd_get_sessions(&raw mut sessions_ptr) };
5757

5858
if result < 0 {
5959
return Err(format!("sd_get_sessions failed: {result}").into());
@@ -71,11 +71,11 @@ mod login {
7171
let session_cstr = unsafe { CStr::from_ptr(session_ptr) };
7272
sessions.push(session_cstr.to_string_lossy().into_owned());
7373

74-
unsafe { libc::free(session_ptr as *mut libc::c_void) };
74+
unsafe { libc::free(session_ptr.cast()) };
7575
i += 1;
7676
}
7777

78-
unsafe { libc::free(sessions_ptr as *mut libc::c_void) };
78+
unsafe { libc::free(sessions_ptr.cast()) };
7979
}
8080

8181
Ok(sessions)
@@ -86,7 +86,7 @@ mod login {
8686
let session_cstring = CString::new(session_id)?;
8787
let mut uid: std::os::raw::c_uint = 0;
8888

89-
let result = unsafe { ffi::sd_session_get_uid(session_cstring.as_ptr(), &mut uid) };
89+
let result = unsafe { ffi::sd_session_get_uid(session_cstring.as_ptr(), &raw mut uid) };
9090

9191
if result < 0 {
9292
return Err(
@@ -102,7 +102,8 @@ mod login {
102102
let session_cstring = CString::new(session_id)?;
103103
let mut usec: u64 = 0;
104104

105-
let result = unsafe { ffi::sd_session_get_start_time(session_cstring.as_ptr(), &mut usec) };
105+
let result =
106+
unsafe { ffi::sd_session_get_start_time(session_cstring.as_ptr(), &raw mut usec) };
106107

107108
if result < 0 {
108109
return Err(format!(
@@ -119,7 +120,7 @@ mod login {
119120
let session_cstring = CString::new(session_id)?;
120121
let mut tty_ptr: *mut libc::c_char = ptr::null_mut();
121122

122-
let result = unsafe { ffi::sd_session_get_tty(session_cstring.as_ptr(), &mut tty_ptr) };
123+
let result = unsafe { ffi::sd_session_get_tty(session_cstring.as_ptr(), &raw mut tty_ptr) };
123124

124125
if result < 0 {
125126
return Err(
@@ -134,7 +135,7 @@ mod login {
134135
let tty_cstr = unsafe { CStr::from_ptr(tty_ptr) };
135136
let tty_string = tty_cstr.to_string_lossy().into_owned();
136137

137-
unsafe { libc::free(tty_ptr as *mut libc::c_void) };
138+
unsafe { libc::free(tty_ptr.cast()) };
138139

139140
Ok(Some(tty_string))
140141
}
@@ -147,7 +148,7 @@ mod login {
147148
let mut host_ptr: *mut libc::c_char = ptr::null_mut();
148149

149150
let result =
150-
unsafe { ffi::sd_session_get_remote_host(session_cstring.as_ptr(), &mut host_ptr) };
151+
unsafe { ffi::sd_session_get_remote_host(session_cstring.as_ptr(), &raw mut host_ptr) };
151152

152153
if result < 0 {
153154
return Err(format!(
@@ -163,7 +164,7 @@ mod login {
163164
let host_cstr = unsafe { CStr::from_ptr(host_ptr) };
164165
let host_string = host_cstr.to_string_lossy().into_owned();
165166

166-
unsafe { libc::free(host_ptr as *mut libc::c_void) };
167+
unsafe { libc::free(host_ptr.cast()) };
167168

168169
Ok(Some(host_string))
169170
}
@@ -176,7 +177,7 @@ mod login {
176177
let mut display_ptr: *mut libc::c_char = ptr::null_mut();
177178

178179
let result =
179-
unsafe { ffi::sd_session_get_display(session_cstring.as_ptr(), &mut display_ptr) };
180+
unsafe { ffi::sd_session_get_display(session_cstring.as_ptr(), &raw mut display_ptr) };
180181

181182
if result < 0 {
182183
return Err(format!(
@@ -192,7 +193,7 @@ mod login {
192193
let display_cstr = unsafe { CStr::from_ptr(display_ptr) };
193194
let display_string = display_cstr.to_string_lossy().into_owned();
194195

195-
unsafe { libc::free(display_ptr as *mut libc::c_void) };
196+
unsafe { libc::free(display_ptr.cast()) };
196197

197198
Ok(Some(display_string))
198199
}
@@ -204,7 +205,8 @@ mod login {
204205
let session_cstring = CString::new(session_id)?;
205206
let mut type_ptr: *mut libc::c_char = ptr::null_mut();
206207

207-
let result = unsafe { ffi::sd_session_get_type(session_cstring.as_ptr(), &mut type_ptr) };
208+
let result =
209+
unsafe { ffi::sd_session_get_type(session_cstring.as_ptr(), &raw mut type_ptr) };
208210

209211
if result < 0 {
210212
return Err(
@@ -219,7 +221,7 @@ mod login {
219221
let type_cstr = unsafe { CStr::from_ptr(type_ptr) };
220222
let type_string = type_cstr.to_string_lossy().into_owned();
221223

222-
unsafe { libc::free(type_ptr as *mut libc::c_void) };
224+
unsafe { libc::free(type_ptr.cast()) };
223225

224226
Ok(Some(type_string))
225227
}
@@ -231,7 +233,8 @@ mod login {
231233
let session_cstring = CString::new(session_id)?;
232234
let mut seat_ptr: *mut libc::c_char = ptr::null_mut();
233235

234-
let result = unsafe { ffi::sd_session_get_seat(session_cstring.as_ptr(), &mut seat_ptr) };
236+
let result =
237+
unsafe { ffi::sd_session_get_seat(session_cstring.as_ptr(), &raw mut seat_ptr) };
235238

236239
if result < 0 {
237240
return Err(
@@ -246,7 +249,7 @@ mod login {
246249
let seat_cstr = unsafe { CStr::from_ptr(seat_ptr) };
247250
let seat_string = seat_cstr.to_string_lossy().into_owned();
248251

249-
unsafe { libc::free(seat_ptr as *mut libc::c_void) };
252+
unsafe { libc::free(seat_ptr.cast()) };
250253

251254
Ok(Some(seat_string))
252255
}
@@ -373,9 +376,9 @@ pub fn read_login_records() -> UResult<Vec<SystemdLoginRecord>> {
373376
let ret = libc::getpwuid_r(
374377
uid,
375378
passwd.as_mut_ptr(),
376-
buf.as_mut_ptr() as *mut libc::c_char,
379+
buf.as_mut_ptr().cast(),
377380
buf.len(),
378-
&mut result,
381+
&raw mut result,
379382
);
380383

381384
if ret == 0 && !result.is_null() {

src/uucore/src/lib/features/uptime.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ pub fn get_uptime(_boot_time: Option<time_t>) -> UResult<i64> {
6262
tv_sec: 0,
6363
tv_nsec: 0,
6464
};
65-
let raw_tp = &mut tp as *mut timespec;
6665

6766
// OpenBSD prototype: clock_gettime(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int;
68-
let ret: c_int = unsafe { clock_gettime(CLOCK_BOOTTIME, raw_tp) };
67+
let ret: c_int = unsafe { clock_gettime(CLOCK_BOOTTIME, &raw mut tp) };
6968

7069
if ret == 0 {
7170
#[cfg(target_pointer_width = "64")]

src/uucore/src/lib/features/utmpx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl Iterator for UtmpxIter {
525525
// All the strings live inline in the struct as arrays, which
526526
// makes things easier.
527527
Some(UtmpxRecord::Traditional(Box::new(Utmpx {
528-
inner: ptr::read(res as *const _),
528+
inner: ptr::read(res.cast_const()),
529529
})))
530530
}
531531
}

tests/uutests/src/lib/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ impl AtPath {
11471147
unsafe {
11481148
let name = CString::new(self.plus_as_string(fifo)).unwrap();
11491149
let mut stat: libc::stat = std::mem::zeroed();
1150-
if libc::stat(name.as_ptr(), &mut stat) >= 0 {
1150+
if libc::stat(name.as_ptr(), &raw mut stat) >= 0 {
11511151
libc::S_IFIFO & stat.st_mode as libc::mode_t != 0
11521152
} else {
11531153
false
@@ -1160,7 +1160,7 @@ impl AtPath {
11601160
unsafe {
11611161
let name = CString::new(self.plus_as_string(char_dev)).unwrap();
11621162
let mut stat: libc::stat = std::mem::zeroed();
1163-
if libc::stat(name.as_ptr(), &mut stat) >= 0 {
1163+
if libc::stat(name.as_ptr(), &raw mut stat) >= 0 {
11641164
libc::S_IFCHR & stat.st_mode as libc::mode_t != 0
11651165
} else {
11661166
false

0 commit comments

Comments
 (0)