Skip to content

Commit 8246395

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
Warn on clippy::cast_possible_wrap
Fixes #3198 Signed-off-by: Jonathan Browne <[email protected]>
1 parent d0b0000 commit 8246395

File tree

13 files changed

+46
-19
lines changed

13 files changed

+46
-19
lines changed

.cargo/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ rustflags = [
55
"-Dclippy::ptr_as_ptr",
66
"-Dclippy::undocumented_unsafe_blocks",
77
"-Dclippy::cast_lossless",
8+
"-Dclippy::cast_possible_wrap",
89
"-Dclippy::cast_sign_loss",
910
"-Dmissing_debug_implementations"
1011
]

src/jailer/src/resource_limits.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ impl From<Resource> for u32 {
2727
fn from(resource: Resource) -> u32 {
2828
match resource {
2929
#[allow(clippy::unnecessary_cast)]
30+
#[allow(clippy::cast_possible_wrap)]
3031
// Definition of libc::RLIMIT_FSIZE depends on the target_env:
3132
// * when equals to "musl" -> libc::RLIMIT_FSIZE is a c_int (which is an i32)
3233
// * when equals to "gnu" -> libc::RLIMIT_FSIZE is __rlimit_resource_t which is a
3334
// c_uint (which is an u32)
3435
Resource::RlimitFsize => libc::RLIMIT_FSIZE as u32,
3536
#[allow(clippy::unnecessary_cast)]
37+
#[allow(clippy::cast_possible_wrap)]
3638
// Definition of libc::RLIMIT_NOFILE depends on the target_env:
3739
// * when equals to "musl" -> libc::RLIMIT_NOFILE is a c_int (which is an i32)
3840
// * when equals to "gnu" -> libc::RLIMIT_NOFILE is __rlimit_resource_t which is a
@@ -42,6 +44,27 @@ impl From<Resource> for u32 {
4244
}
4345
}
4446

47+
impl From<Resource> for i32 {
48+
fn from(resource: Resource) -> i32 {
49+
match resource {
50+
#[allow(clippy::unnecessary_cast)]
51+
#[allow(clippy::cast_possible_wrap)]
52+
// Definition of libc::RLIMIT_FSIZE depends on the target_env:
53+
// * when equals to "musl" -> libc::RLIMIT_FSIZE is a c_int (which is an i32)
54+
// * when equals to "gnu" -> libc::RLIMIT_FSIZE is __rlimit_resource_t which is a
55+
// c_uint (which is an u32)
56+
Resource::RlimitFsize => libc::RLIMIT_FSIZE as i32,
57+
#[allow(clippy::unnecessary_cast)]
58+
#[allow(clippy::cast_possible_wrap)]
59+
// Definition of libc::RLIMIT_NOFILE depends on the target_env:
60+
// * when equals to "musl" -> libc::RLIMIT_NOFILE is a c_int (which is an i32)
61+
// * when equals to "gnu" -> libc::RLIMIT_NOFILE is __rlimit_resource_t which is a
62+
// c_uint (which is an u32)
63+
Resource::RlimitNoFile => libc::RLIMIT_NOFILE as i32,
64+
}
65+
}
66+
}
67+
4568
impl Display for Resource {
4669
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
4770
match self {
@@ -86,7 +109,7 @@ impl ResourceLimits {
86109

87110
// SAFETY: Safe because `resource` is a known-valid constant, and `&rlim`
88111
// is non-dangling.
89-
SyscallReturnCode(unsafe { libc::setrlimit(u32::from(resource) as _, &rlim) })
112+
SyscallReturnCode(unsafe { libc::setrlimit(resource.into(), &rlim) })
90113
.into_empty_result()
91114
.map_err(|_| Error::Setrlimit(resource.to_string()))
92115
}
@@ -148,7 +171,7 @@ mod tests {
148171
rlim_cur: 0,
149172
rlim_max: 0,
150173
};
151-
unsafe { libc::getrlimit(u32::from(resource) as _, &mut rlim) };
174+
unsafe { libc::getrlimit(resource.into(), &mut rlim) };
152175
assert_ne!(rlim.rlim_cur, new_limit);
153176
assert_ne!(rlim.rlim_max, new_limit);
154177

@@ -160,7 +183,7 @@ mod tests {
160183
rlim_cur: 0,
161184
rlim_max: 0,
162185
};
163-
unsafe { libc::getrlimit(u32::from(resource) as _, &mut rlim) };
186+
unsafe { libc::getrlimit(resource.into(), &mut rlim) };
164187
assert_eq!(rlim.rlim_cur, new_limit);
165188
assert_eq!(rlim.rlim_max, new_limit);
166189
}

src/logger/src/metrics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,8 @@ impl SerializeToUtcTimestampMs {
11481148
impl Serialize for SerializeToUtcTimestampMs {
11491149
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
11501150
serializer.serialize_i64(
1151-
utils::time::get_time_ns(utils::time::ClockType::Real) as i64 / 1_000_000,
1151+
i64::try_from(utils::time::get_time_ns(utils::time::ClockType::Real) / 1_000_000)
1152+
.unwrap(),
11521153
)
11531154
}
11541155
}

src/utils/src/byte_order.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ generate_read_fn!(read_be_u32, u32, u8, 4, from_be_bytes);
3838

3939
generate_write_fn!(write_le_u16, u16, u8, to_le_bytes);
4040
generate_write_fn!(write_le_u32, u32, u8, to_le_bytes);
41+
generate_write_fn!(write_le_u32_to_i8, u32, i8, to_le_bytes);
4142
generate_write_fn!(write_le_u64, u64, u8, to_le_bytes);
4243
generate_write_fn!(write_le_i32, i32, i8, to_le_bytes);
4344

src/utils/src/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub fn get_time_ms(clock_type: ClockType) -> u64 {
181181
///
182182
/// * `value` - Timestamp in seconds.
183183
pub fn seconds_to_nanoseconds(value: i64) -> Option<i64> {
184-
value.checked_mul(NANOS_PER_SECOND as i64)
184+
value.checked_mul(i64::try_from(NANOS_PER_SECOND).unwrap())
185185
}
186186

187187
#[cfg(test)]

src/utils/src/vm_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn build_guarded_region(
8686
prot,
8787
flags | libc::MAP_FIXED,
8888
fd,
89-
offset as libc::off_t,
89+
libc::off_t::try_from(offset).unwrap(),
9090
)
9191
};
9292

src/vmm/src/arch/x86_64/interrupts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn get_klapic_reg(klapic: &kvm_lapic_state, reg_offset: usize) -> u32 {
3535
fn set_klapic_reg(klapic: &mut kvm_lapic_state, reg_offset: usize, value: u32) {
3636
let range = reg_offset..reg_offset + 4;
3737
let reg = klapic.regs.get_mut(range).expect("set_klapic_reg range");
38-
byte_order::write_le_i32(reg, value as i32)
38+
byte_order::write_le_u32_to_i8(reg, value)
3939
}
4040

4141
fn set_apic_delivery_mode(reg: u32, mode: u32) -> u32 {

src/vmm/src/arch/x86_64/mptable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ pub fn setup_mptable(mem: &GuestMemoryMmap, num_cpus: u8) -> Result<(), MptableE
285285
mpc_table.0.productid = MPC_PRODUCT_ID;
286286
mpc_table.0.lapic = APIC_DEFAULT_PHYS_BASE;
287287
checksum = checksum.wrapping_add(compute_checksum(&mpc_table.0));
288-
mpc_table.0.checksum = (!checksum).wrapping_add(1) as i8;
288+
#[allow(clippy::cast_possible_wrap)]
289+
let checksum_final = (!checksum).wrapping_add(1) as i8;
290+
mpc_table.0.checksum = checksum_final;
289291
mem.write_obj(mpc_table, table_base)
290292
.map_err(|_| MptableError::WriteMpcTable)?;
291293
}

src/vmm/src/devices/virtio/net/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl Net {
196196
)
197197
.map_err(NetError::TapSetOffload)?;
198198

199-
let vnet_hdr_size = vnet_hdr_len() as i32;
199+
let vnet_hdr_size = i32::try_from(vnet_hdr_len()).unwrap();
200200
tap.set_vnet_hdr_size(vnet_hdr_size)
201201
.map_err(NetError::TapSetVnetHdrSize)?;
202202

src/vmm/src/devices/virtio/net/tap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl Tap {
193193

194194
/// Write an `IoVecBuffer` to tap
195195
pub(crate) fn write_iovec(&mut self, buffer: &IoVecBuffer) -> Result<usize, IoError> {
196-
let iovcnt = buffer.iovec_count() as i32;
196+
let iovcnt = i32::try_from(buffer.iovec_count()).unwrap();
197197
let iov = buffer.as_iovec_ptr();
198198

199199
// SAFETY: `writev` is safe. Called with a valid tap fd, the iovec pointer and length

0 commit comments

Comments
 (0)