]> git.lizzy.rs Git - rust.git/commitdiff
use Memory::read_c_str, avoid a few to_ptr
authorRalf Jung <post@ralfj.de>
Fri, 5 Jul 2019 06:37:19 +0000 (08:37 +0200)
committerRalf Jung <post@ralfj.de>
Sat, 6 Jul 2019 09:01:07 +0000 (11:01 +0200)
src/shims/foreign_items.rs

index 9dbb55668ef35dc707ab4fdbce71e8877250a7eb..41966c5f0d9cb91c935c0862969ea1a4661810d9 100644 (file)
@@ -318,8 +318,8 @@ fn emulate_foreign_item(
 
             "dlsym" => {
                 let _handle = this.read_scalar(args[0])?;
-                let symbol = this.read_scalar(args[1])?.to_ptr()?;
-                let symbol_name = this.memory().get(symbol.alloc_id)?.read_c_str(tcx, symbol)?;
+                let symbol = this.read_scalar(args[1])?.not_undef()?;
+                let symbol_name = this.memory().read_c_str(symbol)?;
                 let err = format!("bad c unicode symbol: {:?}", symbol_name);
                 let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
                 if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
@@ -433,8 +433,8 @@ fn emulate_foreign_item(
 
             "getenv" => {
                 let result = {
-                    let name_ptr = this.read_scalar(args[0])?.to_ptr()?;
-                    let name = this.memory().get(name_ptr.alloc_id)?.read_c_str(tcx, name_ptr)?;
+                    let name_ptr = this.read_scalar(args[0])?.not_undef()?;
+                    let name = this.memory().read_c_str(name_ptr)?;
                     match this.machine.env_vars.get(name) {
                         Some(&var) => Scalar::Ptr(var),
                         None => Scalar::ptr_null(&*this.tcx),
@@ -448,12 +448,7 @@ fn emulate_foreign_item(
                 {
                     let name_ptr = this.read_scalar(args[0])?.not_undef()?;
                     if !this.is_null(name_ptr)? {
-                        let name_ptr = name_ptr.to_ptr()?;
-                        let name = this
-                            .memory()
-                            .get(name_ptr.alloc_id)?
-                            .read_c_str(tcx, name_ptr)?
-                            .to_owned();
+                        let name = this.memory().read_c_str(name_ptr)?.to_owned();
                         if !name.is_empty() && !name.contains(&b'=') {
                             success = Some(this.machine.env_vars.remove(&name));
                         }
@@ -473,11 +468,10 @@ fn emulate_foreign_item(
                 let mut new = None;
                 {
                     let name_ptr = this.read_scalar(args[0])?.not_undef()?;
-                    let value_ptr = this.read_scalar(args[1])?.to_ptr()?;
-                    let value = this.memory().get(value_ptr.alloc_id)?.read_c_str(tcx, value_ptr)?;
+                    let value_ptr = this.read_scalar(args[1])?.not_undef()?;
+                    let value = this.memory().read_c_str(value_ptr)?;
                     if !this.is_null(name_ptr)? {
-                        let name_ptr = name_ptr.to_ptr()?;
-                        let name = this.memory().get(name_ptr.alloc_id)?.read_c_str(tcx, name_ptr)?;
+                        let name = this.memory().read_c_str(name_ptr)?;
                         if !name.is_empty() && !name.contains(&b'=') {
                             new = Some((name.to_owned(), value.to_owned()));
                         }
@@ -552,8 +546,8 @@ fn emulate_foreign_item(
             }
 
             "strlen" => {
-                let ptr = this.read_scalar(args[0])?.to_ptr()?;
-                let n = this.memory().get(ptr.alloc_id)?.read_c_str(tcx, ptr)?.len();
+                let ptr = this.read_scalar(args[0])?.not_undef()?;
+                let n = this.memory().read_c_str(ptr)?.len();
                 this.write_scalar(Scalar::from_uint(n as u64, dest.layout.size), dest)?;
             }