]> git.lizzy.rs Git - rust.git/blobdiff - src/shims/mod.rs
avoid using unchecked casts or arithmetic
[rust.git] / src / shims / mod.rs
index 6adf01385855004b4477839ede404f531ea0203b..d9e4d226ecc9a6d428751be0f3b08baf3c3f71e1 100644 (file)
@@ -7,9 +7,12 @@
 pub mod time;
 pub mod tls;
 
-use crate::*;
+use std::convert::TryFrom;
+
 use rustc::{mir, ty};
 
+use crate::*;
+
 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
     fn find_mir_or_eval_fn(
@@ -54,8 +57,7 @@ fn align_offset(
         let (dest, ret) = ret.unwrap();
 
         let req_align = this
-            .force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?
-            as usize;
+            .force_bits(this.read_scalar(align_op)?.not_undef()?, this.pointer_size())?;
 
         // Stop if the alignment is not a power of two.
         if !req_align.is_power_of_two() {
@@ -69,12 +71,15 @@ fn align_offset(
         if let Ok(ptr) = this.force_ptr(ptr_scalar) {
             // Only do anything if we can identify the allocation this goes to.
             let cur_align =
-                this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes()
-                    as usize;
-            if cur_align >= req_align {
+                this.memory.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)?.1.bytes();
+            if u128::from(cur_align) >= req_align {
                 // If the allocation alignment is at least the required alignment we use the
-                // libcore implementation
-                result = (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8).align_offset(req_align) as u128;
+                // libcore implementation.
+                // FIXME: is this correct in case of truncation?
+                result = u128::try_from(
+                    (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
+                        .align_offset(usize::try_from(req_align).unwrap())
+                ).unwrap();
             }
         }