]> git.lizzy.rs Git - rust.git/commitdiff
pass clippy::cast_possible_truncation
authorRalf Jung <post@ralfj.de>
Sat, 23 Jul 2022 12:44:16 +0000 (08:44 -0400)
committerRalf Jung <post@ralfj.de>
Sat, 23 Jul 2022 13:14:13 +0000 (09:14 -0400)
src/lib.rs
src/shims/backtrace.rs
src/shims/foreign_items.rs
src/shims/intrinsics/mod.rs
src/shims/unix/fs.rs
src/shims/windows/dlsym.rs
src/shims/windows/foreign_items.rs

index c1b0c4afca68c0885177789c3acf4ab6ed5873e2..caae17b202235e7248f25bc0d59381222a6f0b7d 100644 (file)
@@ -29,6 +29,7 @@
     clippy::cast_possible_wrap, // unsigned -> signed
     clippy::cast_sign_loss, // signed -> unsigned
     clippy::cast_lossless,
+    clippy::cast_possible_truncation,
 )]
 
 extern crate rustc_apfloat;
index 54ab8665ce35c706c4a355e55ae59b8dea1c2179..9182b2a72173b912dc84e4fcaada16c7d8f92dd7 100644 (file)
@@ -171,9 +171,11 @@ fn handle_miri_resolve_frame(
             );
         }
 
-        let lineno: u32 = lo.line as u32;
+        // `u32` is not enough to fit line/colno, which can be `usize`. It seems unlikely that a
+        // file would have more than 2^32 lines or columns, but whatever, just default to 0.
+        let lineno: u32 = u32::try_from(lo.line).unwrap_or(0);
         // `lo.col` is 0-based - add 1 to make it 1-based for the caller.
-        let colno: u32 = lo.col.0 as u32 + 1;
+        let colno: u32 = u32::try_from(lo.col.0 + 1).unwrap_or(0);
 
         let dest = this.force_allocation(dest)?;
         if let ty::Adt(adt, _) = dest.layout.ty.kind() {
index 317eab082c0c2c871a6c069721cdae6058e2c9b7..208e7ea788f7dbf2c880b6916d09291b1af27f85 100644 (file)
@@ -82,8 +82,12 @@ fn malloc(
             let align = this.min_align(size, kind);
             let ptr = this.allocate_ptr(Size::from_bytes(size), align, kind.into())?;
             if zero_init {
-                // We just allocated this, the access is definitely in-bounds.
-                this.write_bytes_ptr(ptr.into(), iter::repeat(0u8).take(size as usize)).unwrap();
+                // We just allocated this, the access is definitely in-bounds and fits into our address space.
+                this.write_bytes_ptr(
+                    ptr.into(),
+                    iter::repeat(0u8).take(usize::try_from(size).unwrap()),
+                )
+                .unwrap();
             }
             Ok(ptr.into())
         }
@@ -529,7 +533,7 @@ fn emulate_foreign_item_by_name(
                 let val = this.read_scalar(val)?.to_i32()?;
                 let num = this.read_scalar(num)?.to_machine_usize(this)?;
                 // The docs say val is "interpreted as unsigned char".
-                #[allow(clippy::cast_sign_loss)]
+                #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
                 let val = val as u8;
 
                 if let Some(idx) = this
@@ -550,7 +554,7 @@ fn emulate_foreign_item_by_name(
                 let val = this.read_scalar(val)?.to_i32()?;
                 let num = this.read_scalar(num)?.to_machine_usize(this)?;
                 // The docs say val is "interpreted as unsigned char".
-                #[allow(clippy::cast_sign_loss)]
+                #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
                 let val = val as u8;
 
                 let idx = this
index c07ed8b294891866c307e450f36c285492584173..4c2d08ffceabce8d6f29b88958a4fa2652ef41ed 100644 (file)
@@ -117,10 +117,7 @@ fn emulate_intrinsic_by_name(
                 let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
                     err_ub_format!("overflow computing total size of `{intrinsic_name}`")
                 })?;
-                this.write_bytes_ptr(
-                    ptr,
-                    iter::repeat(val_byte).take(byte_count.bytes() as usize),
-                )?;
+                this.write_bytes_ptr(ptr, iter::repeat(val_byte).take(byte_count.bytes_usize()))?;
             }
 
             // Floating-point operations
index 3b82cb3c4eb8a9e71553873c344a089b551a9337..dc31237a3199751ed18d67313f5c000a995775e4 100644 (file)
@@ -785,8 +785,8 @@ fn read(
             trace!("read: FD mapped to {:?}", file_descriptor);
             // We want to read at most `count` bytes. We are sure that `count` is not negative
             // because it was a target's `usize`. Also we are sure that its smaller than
-            // `usize::MAX` because it is a host's `isize`.
-            let mut bytes = vec![0; count as usize];
+            // `usize::MAX` because it is bounded by the host's `isize`.
+            let mut bytes = vec![0; usize::try_from(count).unwrap()];
             // `File::read` never returns a value larger than `count`,
             // so this cannot fail.
             let result =
index 51b0e3b83d4733c19687aad079ef09b85c196fd5..eab5f99c8785170a055b3557515cc5a0375e4e9a 100644 (file)
@@ -84,7 +84,8 @@ fn call_dlsym(
                     } else {
                         io::stderr().write(buf_cont)
                     };
-                    res.ok().map(|n| n as u32)
+                    // We write at most `n` bytes, which is a `u32`, so we cannot have written more than that.
+                    res.ok().map(|n| u32::try_from(n).unwrap())
                 } else {
                     throw_unsup_format!(
                         "on Windows, writing to anything except stdout/stderr is not supported"
index 3f4b8b14002e78d183317995f47ea049159045b3..29afe52cafd6c003b0c9a494fdc5472975f82c1f 100644 (file)
@@ -116,7 +116,7 @@ fn emulate_foreign_item_by_name(
                 // Initialize with `0`.
                 this.write_bytes_ptr(
                     system_info.ptr,
-                    iter::repeat(0u8).take(system_info.layout.size.bytes() as usize),
+                    iter::repeat(0u8).take(system_info.layout.size.bytes_usize()),
                 )?;
                 // Set selected fields.
                 let word_layout = this.machine.layouts.u16;