diff options
-rw-r--r-- | cli/tests/unit/copy_file_test.ts | 11 | ||||
-rw-r--r-- | ext/fs/std_fs.rs | 13 |
2 files changed, 19 insertions, 5 deletions
diff --git a/cli/tests/unit/copy_file_test.ts b/cli/tests/unit/copy_file_test.ts index 1c967b043..ad467f510 100644 --- a/cli/tests/unit/copy_file_test.ts +++ b/cli/tests/unit/copy_file_test.ts @@ -236,3 +236,14 @@ Deno.test( copyFileSyncMode("Hello world!".repeat(128 * 1024)); }, ); + +Deno.test( + { permissions: { read: true, write: true } }, + async function copyFileNulPath() { + const fromFilename = "from.txt\0"; + const toFilename = "to.txt\0"; + await assertRejects(async () => { + await Deno.copyFile(fromFilename, toFilename); + }, TypeError); + }, +); diff --git a/ext/fs/std_fs.rs b/ext/fs/std_fs.rs index bb8d114a0..c1c9200cb 100644 --- a/ext/fs/std_fs.rs +++ b/ext/fs/std_fs.rs @@ -413,10 +413,11 @@ fn copy_file(from: &Path, to: &Path) -> FsResult<()> { use std::io::Read; use std::os::unix::fs::OpenOptionsExt; use std::os::unix::fs::PermissionsExt; - use std::os::unix::prelude::OsStrExt; - let from_str = CString::new(from.as_os_str().as_bytes()).unwrap(); - let to_str = CString::new(to.as_os_str().as_bytes()).unwrap(); + let from_str = CString::new(from.as_os_str().as_encoded_bytes()) + .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; + let to_str = CString::new(to.as_os_str().as_encoded_bytes()) + .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; // SAFETY: `from` and `to` are valid C strings. // std::fs::copy does open() + fcopyfile() on macOS. We try to use @@ -555,8 +556,10 @@ fn cp(from: &Path, to: &Path) -> FsResult<()> { use std::ffi::CString; use std::os::unix::ffi::OsStrExt; - let from_str = CString::new(from.as_os_str().as_bytes()).unwrap(); - let to_str = CString::new(to.as_os_str().as_bytes()).unwrap(); + let from_str = CString::new(from.as_os_str().as_bytes()) + .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; + let to_str = CString::new(to.as_os_str().as_bytes()) + .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; // SAFETY: `from` and `to` are valid C strings. unsafe { |