]> git.lizzy.rs Git - rust.git/commitdiff
`to_u64` and `to_bytes` are horribly easy to use wrongly.
authorOliver Schneider <github35764891676564198441@oli-obk.de>
Wed, 30 May 2018 15:43:54 +0000 (17:43 +0200)
committerOliver Schneider <github35764891676564198441@oli-obk.de>
Wed, 30 May 2018 15:43:54 +0000 (17:43 +0200)
src/fn_call.rs
src/lib.rs

index fdaa819c08f7ad456a4cc0ed51d1560524e79a94..72efcd6ede077a27148049d32c4b954433c05a4e 100644 (file)
@@ -316,7 +316,7 @@ fn call_c_abi(
 
             "memrchr" => {
                 let ptr = self.into_ptr(args[0].value)?;
-                let val = self.value_to_scalar(args[1])?.to_u64()? as u8;
+                let val = self.value_to_scalar(args[1])?.to_bytes()? as u8;
                 let num = self.value_to_scalar(args[2])?.to_u64()?;
                 if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().rev().position(
                     |&c| c == val,
@@ -331,7 +331,7 @@ fn call_c_abi(
 
             "memchr" => {
                 let ptr = self.into_ptr(args[0].value)?;
-                let val = self.value_to_scalar(args[1])?.to_u64()? as u8;
+                let val = self.value_to_scalar(args[1])?.to_bytes()? as u8;
                 let num = self.value_to_scalar(args[2])?.to_u64()?;
                 if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().position(
                     |&c| c == val,
@@ -414,9 +414,9 @@ fn call_c_abi(
             }
 
             "write" => {
-                let fd = self.value_to_scalar(args[0])?.to_u64()?;
+                let fd = self.value_to_scalar(args[0])?.to_bytes()?;
                 let buf = self.into_ptr(args[1].value)?;
-                let n = self.value_to_scalar(args[2])?.to_u64()?;
+                let n = self.value_to_scalar(args[2])?.to_bytes()? as u64;
                 trace!("Called write({:?}, {:?}, {:?})", fd, buf, n);
                 let result = if fd == 1 || fd == 2 {
                     // stdout/stderr
index 6d7c0b05f80f7e9d4dfd2068324a4e970b7114f3..550965573cb47112ec1dd9f2cb5262f11ef0c8d0 100644 (file)
@@ -64,6 +64,9 @@ pub trait ScalarExt {
     fn from_f64(f: f64) -> Self;
     fn to_u64(self) -> EvalResult<'static, u64>;
     fn is_null(self) -> EvalResult<'static, bool>;
+    /// HACK: this function just extracts all bits if `defined != 0`
+    /// Mainly used for args of C-functions and we should totally correctly fetch the size
+    /// of their arguments
     fn to_bytes(self) -> EvalResult<'static, u128>;
 }