Skip to content

Commit 5ee7998

Browse files
authored
fix some cast clippy (#333)
2 parents 7e3e936 + c89a5a0 commit 5ee7998

File tree

18 files changed

+211
-92
lines changed

18 files changed

+211
-92
lines changed

core/src/common/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub enum Syscall {
8888
connect,
8989
listen,
9090
accept,
91+
#[cfg(windows)]
92+
WSAAccept,
9193
shutdown,
9294
close,
9395
socket,

core/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ pub mod net;
8080
clippy::similar_names,
8181
clippy::not_unsafe_ptr_arg_deref,
8282
clippy::many_single_char_names,
83-
clippy::cast_sign_loss,
84-
clippy::cast_possible_truncation,
85-
clippy::cast_possible_wrap,
83+
clippy::useless_conversion,
8684
clippy::unnecessary_cast,
8785
trivial_numeric_casts
8886
)]

core/src/syscall/unix/mod.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ macro_rules! impl_nio_read_buf {
201201
);
202202
if r != -1 {
203203
$crate::syscall::reset_errno();
204-
received += r as size_t;
204+
received += libc::size_t::try_from(r).expect("r overflow");
205205
if received >= $len || r == 0 {
206-
r = received as ssize_t;
206+
r = received.try_into().expect("received overflow");
207207
break;
208208
}
209209
}
@@ -219,7 +219,7 @@ macro_rules! impl_nio_read_buf {
219219
$fd,
220220
Some(wait_time)
221221
).is_err() {
222-
r = received as ssize_t;
222+
r = received.try_into().expect("received overflow");
223223
break;
224224
}
225225
} else if error_kind != std::io::ErrorKind::Interrupted {
@@ -262,8 +262,8 @@ macro_rules! impl_nio_read_iovec {
262262
let vec = unsafe {
263263
Vec::from_raw_parts(
264264
$iov.cast_mut(),
265-
$iovcnt as usize,
266-
$iovcnt as usize
265+
$iovcnt.try_into().expect("overflow"),
266+
$iovcnt.try_into().expect("overflow"),
267267
)
268268
};
269269
let mut length = 0;
@@ -298,17 +298,17 @@ macro_rules! impl_nio_read_iovec {
298298
$($arg, )*
299299
);
300300
if r == 0 {
301-
r = received as ssize_t;
301+
r = received.try_into().expect("received overflow");
302302
std::mem::forget(vec);
303303
if blocking {
304304
$crate::syscall::set_blocking($fd);
305305
}
306306
return r;
307307
} else if r != -1 {
308308
$crate::syscall::reset_errno();
309-
received += r as usize;
309+
received += libc::size_t::try_from(r).expect("r overflow");
310310
if received >= length {
311-
r = received as ssize_t;
311+
r = received.try_into().expect("received overflow");
312312
break;
313313
}
314314
offset = received.saturating_sub(length);
@@ -325,7 +325,7 @@ macro_rules! impl_nio_read_iovec {
325325
$fd,
326326
Some(wait_time)
327327
).is_err() {
328-
r = received as ssize_t;
328+
r = received.try_into().expect("received overflow");
329329
std::mem::forget(vec);
330330
if blocking {
331331
$crate::syscall::set_blocking($fd);
@@ -390,9 +390,9 @@ macro_rules! impl_nio_write_buf {
390390
);
391391
if r != -1 {
392392
$crate::syscall::reset_errno();
393-
sent += r as size_t;
393+
sent += libc::size_t::try_from(r).expect("r overflow");
394394
if sent >= $len {
395-
r = sent as ssize_t;
395+
r = sent.try_into().expect("sent overflow");
396396
break;
397397
}
398398
}
@@ -408,7 +408,7 @@ macro_rules! impl_nio_write_buf {
408408
$fd,
409409
Some(wait_time),
410410
).is_err() {
411-
r = sent as ssize_t;
411+
r = sent.try_into().expect("sent overflow");
412412
break;
413413
}
414414
} else if error_kind != std::io::ErrorKind::Interrupted {
@@ -451,8 +451,8 @@ macro_rules! impl_nio_write_iovec {
451451
let vec = unsafe {
452452
Vec::from_raw_parts(
453453
$iov.cast_mut(),
454-
$iovcnt as usize,
455-
$iovcnt as usize
454+
$iovcnt.try_into().expect("overflow"),
455+
$iovcnt.try_into().expect("overflow"),
456456
)
457457
};
458458
let mut length = 0;
@@ -488,9 +488,9 @@ macro_rules! impl_nio_write_iovec {
488488
);
489489
if r != -1 {
490490
$crate::syscall::reset_errno();
491-
sent += r as usize;
491+
sent += libc::size_t::try_from(r).expect("r overflow");
492492
if sent >= length {
493-
r = sent as ssize_t;
493+
r = sent.try_into().expect("sent overflow");
494494
break;
495495
}
496496
offset = sent.saturating_sub(length);
@@ -507,7 +507,7 @@ macro_rules! impl_nio_write_iovec {
507507
$fd,
508508
Some(wait_time)
509509
).is_err() {
510-
r = sent as ssize_t;
510+
r = sent.try_into().expect("sent overflow");
511511
std::mem::forget(vec);
512512
if blocking {
513513
$crate::syscall::set_blocking($fd);
@@ -697,7 +697,7 @@ pub extern "C" fn send_time_limit(fd: c_int) -> u64 {
697697
SEND_TIME_LIMIT.get(&fd).map_or_else(
698698
|| unsafe {
699699
let mut tv: libc::timeval = std::mem::zeroed();
700-
let mut len = size_of::<libc::timeval>() as libc::socklen_t;
700+
let mut len = libc::socklen_t::try_from(size_of::<libc::timeval>()).expect("overflow");
701701
if libc::getsockopt(
702702
fd,
703703
libc::SOL_SOCKET,
@@ -713,9 +713,14 @@ pub extern "C" fn send_time_limit(fd: c_int) -> u64 {
713713
}
714714
panic!("getsockopt failed: {error}");
715715
}
716-
let mut time_limit = (tv.tv_sec as u64)
716+
let mut time_limit = u64::try_from(tv.tv_sec)
717+
.expect("overflow")
717718
.saturating_mul(1_000_000_000)
718-
.saturating_add((tv.tv_usec as u64).saturating_mul(1_000));
719+
.saturating_add(
720+
u64::try_from(tv.tv_usec)
721+
.expect("overflow")
722+
.saturating_mul(1_000),
723+
);
719724
if 0 == time_limit {
720725
// 取消超时
721726
time_limit = u64::MAX;
@@ -732,7 +737,7 @@ pub extern "C" fn recv_time_limit(fd: c_int) -> u64 {
732737
RECV_TIME_LIMIT.get(&fd).map_or_else(
733738
|| unsafe {
734739
let mut tv: libc::timeval = std::mem::zeroed();
735-
let mut len = size_of::<libc::timeval>() as libc::socklen_t;
740+
let mut len = libc::socklen_t::try_from(size_of::<libc::timeval>()).expect("overflow");
736741
if libc::getsockopt(
737742
fd,
738743
libc::SOL_SOCKET,
@@ -748,9 +753,14 @@ pub extern "C" fn recv_time_limit(fd: c_int) -> u64 {
748753
}
749754
panic!("getsockopt failed: {error}");
750755
}
751-
let mut time_limit = (tv.tv_sec as u64)
756+
let mut time_limit = u64::try_from(tv.tv_sec)
757+
.expect("overflow")
752758
.saturating_mul(1_000_000_000)
753-
.saturating_add((tv.tv_usec as u64).saturating_mul(1_000));
759+
.saturating_add(
760+
u64::try_from(tv.tv_usec)
761+
.expect("overflow")
762+
.saturating_mul(1_000),
763+
);
754764
if 0 == time_limit {
755765
// 取消超时
756766
time_limit = u64::MAX;

core/src/syscall/unix/nanosleep.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ impl NanosleepSyscall for NioNanosleepSyscall {
4444
set_errno(libc::EINVAL);
4545
return -1;
4646
}
47-
let time = Duration::new(rqtp.tv_sec as u64, rqtp.tv_nsec as u32);
47+
let time = Duration::new(
48+
rqtp.tv_sec.try_into().expect("overflow"),
49+
rqtp.tv_nsec.try_into().expect("overflow")
50+
);
4851
if let Some(co) = crate::scheduler::SchedulableCoroutine::current() {
4952
let syscall = crate::common::constants::Syscall::nanosleep;
5053
let new_state = crate::common::constants::SyscallState::Suspend(

core/src/syscall/unix/poll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<I: PollSyscall> PollSyscall for NioPollSyscall<I> {
5353
if r != 0 || t == 0 {
5454
break;
5555
}
56-
_ = EventLoops::wait_event(Some(Duration::from_millis(t.min(x) as u64)));
56+
_ = EventLoops::wait_event(Some(Duration::from_millis(t.min(x).try_into().expect("overflow"))));
5757
if t != c_int::MAX {
5858
t = if t > x { t - x } else { 0 };
5959
}

core/src/syscall/unix/pthread_cond_timedwait.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ impl<I: PthreadCondTimedwaitSyscall> PthreadCondTimedwaitSyscall
7676
if abstime.tv_sec < 0 || abstime.tv_nsec < 0 || abstime.tv_nsec > 999_999_999 {
7777
return libc::EINVAL;
7878
}
79-
u64::try_from(Duration::new(abstime.tv_sec as u64, abstime.tv_nsec as u32).as_nanos())
80-
.unwrap_or(u64::MAX)
79+
u64::try_from(Duration::new(
80+
abstime.tv_sec.try_into().expect("overflow"),
81+
abstime.tv_nsec.try_into().expect("overflow")
82+
).as_nanos()
83+
).unwrap_or(u64::MAX)
8184
};
8285
loop {
8386
let mut left_time = abstimeout.saturating_sub(now());
@@ -89,7 +92,7 @@ impl<I: PthreadCondTimedwaitSyscall> PthreadCondTimedwaitSyscall
8992
cond,
9093
lock,
9194
&timespec {
92-
tv_sec: (now() / 1_000_000_000 + 1) as _,
95+
tv_sec: now().saturating_div(1_000_000_000).saturating_add(1).try_into().expect("overflow"),
9396
tv_nsec: 0,
9497
},
9598
);

core/src/syscall/unix/recvmsg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl<I: RecvmsgSyscall> RecvmsgSyscall for NioRecvmsgSyscall<I> {
6969
let vec = unsafe {
7070
Vec::from_raw_parts(
7171
msghdr.msg_iov,
72-
msghdr.msg_iovlen as usize,
73-
msghdr.msg_iovlen as usize,
72+
msghdr.msg_iovlen.try_into().expect("overflow"),
73+
msghdr.msg_iovlen.try_into().expect("overflow"),
7474
)
7575
};
7676
let mut length = 0;
@@ -127,9 +127,9 @@ impl<I: RecvmsgSyscall> RecvmsgSyscall for NioRecvmsgSyscall<I> {
127127
return r;
128128
} else if r != -1 {
129129
reset_errno();
130-
received += r as usize;
130+
received += libc::size_t::try_from(r).expect("r overflow");
131131
if received >= length {
132-
r = received as ssize_t;
132+
r = received.try_into().expect("received overflow");
133133
break;
134134
}
135135
offset = received.saturating_sub(length);

core/src/syscall/unix/select.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ impl<I: SelectSyscall> SelectSyscall for NioSelectSyscall<I> {
6565
let mut t = if timeout.is_null() {
6666
c_uint::MAX
6767
} else {
68-
unsafe { ((*timeout).tv_sec as c_uint) * 1_000_000 + (*timeout).tv_usec as c_uint }
68+
unsafe {
69+
c_uint::try_from((*timeout).tv_sec).expect("overflow")
70+
.saturating_mul(1_000_000)
71+
.saturating_add(c_uint::try_from((*timeout).tv_usec).expect("overflow"))
72+
}
6973
};
7074
let mut o = timeval {
7175
tv_sec: 0,

core/src/syscall/unix/sendmsg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl<I: SendmsgSyscall> SendmsgSyscall for NioSendmsgSyscall<I> {
6969
let vec = unsafe {
7070
Vec::from_raw_parts(
7171
msghdr.msg_iov,
72-
msghdr.msg_iovlen as usize,
73-
msghdr.msg_iovlen as usize,
72+
msghdr.msg_iovlen.try_into().expect("overflow"),
73+
msghdr.msg_iovlen.try_into().expect("overflow"),
7474
)
7575
};
7676
let mut length = 0;
@@ -121,9 +121,9 @@ impl<I: SendmsgSyscall> SendmsgSyscall for NioSendmsgSyscall<I> {
121121
r = self.inner.sendmsg(fn_ptr, fd, &arg, flags);
122122
if r != -1 {
123123
reset_errno();
124-
sent += r as usize;
124+
sent += libc::size_t::try_from(r).expect("r overflow");
125125
if sent >= length {
126-
r = sent as ssize_t;
126+
r = sent.try_into().expect("sent overflow");
127127
break;
128128
}
129129
offset = sent.saturating_sub(length);

core/src/syscall/unix/setsockopt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ impl<I: SetsockoptSyscall> SetsockoptSyscall for NioSetsockoptSyscall<I> {
5353
if 0 == r && libc::SOL_SOCKET == level {
5454
if libc::SO_SNDTIMEO == name {
5555
let tv = unsafe { &*value.cast::<libc::timeval>() };
56-
let mut time_limit = (tv.tv_sec as u64)
56+
let mut time_limit = u64::try_from(tv.tv_sec).expect("overflow")
5757
.saturating_mul(1_000_000_000)
58-
.saturating_add((tv.tv_usec as u64).saturating_mul(1_000));
58+
.saturating_add(u64::try_from(tv.tv_usec).expect("overflow").saturating_mul(1_000));
5959
if 0 == time_limit {
6060
// 取消超时
6161
time_limit = u64::MAX;
6262
}
6363
assert!(SEND_TIME_LIMIT.insert(socket, time_limit).is_none());
6464
} else if libc::SO_RCVTIMEO == name {
6565
let tv = unsafe { &*value.cast::<libc::timeval>() };
66-
let mut time_limit = (tv.tv_sec as u64)
66+
let mut time_limit = u64::try_from(tv.tv_sec).expect("overflow")
6767
.saturating_mul(1_000_000_000)
68-
.saturating_add((tv.tv_usec as u64).saturating_mul(1_000));
68+
.saturating_add(u64::try_from(tv.tv_usec).expect("overflow").saturating_mul(1_000));
6969
if 0 == time_limit {
7070
// 取消超时
7171
time_limit = u64::MAX;

0 commit comments

Comments
 (0)