summary refs log tree commit diff
path: root/host/start-vm
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2023-04-13 09:02:21 +0000
committerAlyssa Ross <hi@alyssa.is>2023-04-13 09:02:21 +0000
commite3525613ae5b3dff11559c9f0abc5e491eb75945 (patch)
tree951471fff56af9faa508cf8de6db354e9bfa0b9c /host/start-vm
parentf7f4c05b1ced423724db3a3580c35b3825caff4c (diff)
downloadspectrum-e3525613ae5b3dff11559c9f0abc5e491eb75945.tar
spectrum-e3525613ae5b3dff11559c9f0abc5e491eb75945.tar.gz
spectrum-e3525613ae5b3dff11559c9f0abc5e491eb75945.tar.bz2
spectrum-e3525613ae5b3dff11559c9f0abc5e491eb75945.tar.lz
spectrum-e3525613ae5b3dff11559c9f0abc5e491eb75945.tar.xz
spectrum-e3525613ae5b3dff11559c9f0abc5e491eb75945.tar.zst
spectrum-e3525613ae5b3dff11559c9f0abc5e491eb75945.zip
start-vm: fix NUL termination in test helper
Signed-off-by: Alyssa Ross <hi@alyssa.is>
Diffstat (limited to 'host/start-vm')
-rw-r--r--host/start-vm/tests/helper.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/host/start-vm/tests/helper.rs b/host/start-vm/tests/helper.rs
index c2d2827..c8921cf 100644
--- a/host/start-vm/tests/helper.rs
+++ b/host/start-vm/tests/helper.rs
@@ -1,5 +1,5 @@
 // SPDX-License-Identifier: EUPL-1.2+
-// SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>
+// SPDX-FileCopyrightText: 2022-2023 Alyssa Ross <hi@alyssa.is>
 
 use std::ffi::OsString;
 use std::io;
@@ -33,13 +33,12 @@ pub struct TempDir(PathBuf);
 
 impl TempDir {
     pub fn new() -> std::io::Result<Self> {
-        let mut dirname = OsString::from("spectrum-start-vm-test-");
-        dirname.push(prog_name());
-        dirname.push(".XXXXXX");
-        let mut template = tmpdir();
-        template.push(dirname);
+        let mut dirname = tmpdir().into_os_string().into_vec();
+        dirname.extend_from_slice(b"/spectrum-start-vm-test-");
+        dirname.extend_from_slice(&prog_name().into_bytes());
+        dirname.extend_from_slice(b".XXXXXX\0");
 
-        let c_path = Box::into_raw(template.into_os_string().into_vec().into_boxed_slice());
+        let c_path = Box::into_raw(dirname.into_boxed_slice());
 
         // Safe because we own c_path.
         if unsafe { mkdtemp(c_path as *mut c_char) }.is_null() {
@@ -47,7 +46,9 @@ impl TempDir {
         }
 
         // Safe because we own c_path and it came from Box::into_raw.
-        let path = PathBuf::from(OsString::from_vec(unsafe { Box::from_raw(c_path) }.into()));
+        let mut buf: Vec<_> = unsafe { Box::from_raw(c_path) }.into();
+        buf.pop(); // Remove the NUL terminator.
+        let path = PathBuf::from(OsString::from_vec(buf));
         Ok(Self(path))
     }