about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-07-06 02:37:56 +0200
committerGitHub <noreply@github.com>2019-07-06 02:37:56 +0200
commit947d7cf16db583bae255260696f022fb0e7ea4db (patch)
tree6b7731c088332535fbb63f216dd3f0588c9d3823
parent182248a466468a7599f21b414904c12daeb8d827 (diff)
parent05f4a574f6409c932475aea0f95b6d8adfe4222b (diff)
downloadrust-947d7cf16db583bae255260696f022fb0e7ea4db.tar.gz
rust-947d7cf16db583bae255260696f022fb0e7ea4db.zip
Rollup merge of #62257 - RalfJung:miri-c-str, r=estebank
forward read_c_str method from Memory to Alloc

This is more convenient to call when one starts with a `Scalar` (which is the common case).

`read_c_str` is only used in Miri.
-rw-r--r--src/librustc_mir/interpret/memory.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs
index 835853cbd21..8607a7c3f1e 100644
--- a/src/librustc_mir/interpret/memory.rs
+++ b/src/librustc_mir/interpret/memory.rs
@@ -728,6 +728,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
 
 /// Reading and writing.
 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
+    /// Reads the given number of bytes from memory. Returns them as a slice.
+    ///
     /// Performs appropriate bounds checks.
     pub fn read_bytes(
         &self,
@@ -741,6 +743,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
         self.get(ptr.alloc_id)?.get_bytes(self, ptr, size)
     }
 
+    /// Reads a 0-terminated sequence of bytes from memory. Returns them as a slice.
+    ///
+    /// Performs appropriate bounds checks.
+    pub fn read_c_str(&self, ptr: Scalar<M::PointerTag>) -> InterpResult<'tcx, &[u8]> {
+        let ptr = self.force_ptr(ptr)?; // We need to read at least 1 byte, so we *need* a ptr.
+        self.get(ptr.alloc_id)?.read_c_str(self, ptr)
+    }
+
     /// Performs appropriate bounds checks.
     pub fn copy(
         &mut self,