]> git.lizzy.rs Git - rust.git/commitdiff
fix length of slice returned from read_c_str
authorRalf Jung <post@ralfj.de>
Sun, 25 Nov 2018 11:07:20 +0000 (12:07 +0100)
committerRalf Jung <post@ralfj.de>
Sun, 25 Nov 2018 11:07:20 +0000 (12:07 +0100)
src/librustc/mir/interpret/allocation.rs

index 3ff0e9f177ddc62b3135b172711f45eba4d71b63..0ecec753398612183fe49157b2c7c6bea0022c40 100644 (file)
@@ -172,9 +172,11 @@ pub fn read_c_str(
         let offset = ptr.offset.bytes() as usize;
         match self.bytes[offset..].iter().position(|&c| c == 0) {
             Some(size) => {
-                let size = Size::from_bytes((size + 1) as u64);
-                // Go through `get_bytes` for checks and AllocationExtra hooks
-                self.get_bytes(cx, ptr, size)
+                let size_with_null = Size::from_bytes((size + 1) as u64);
+                // Go through `get_bytes` for checks and AllocationExtra hooks.
+                // We read the null, so we include it in the requestm, but we want it removed
+                // from the result!
+                Ok(&self.get_bytes(cx, ptr, size_with_null)?[..size])
             }
             None => err!(UnterminatedCString(ptr.erase_tag())),
         }