Skip to content

Commit

Permalink
fix(fs): copyFile NUL path on macOS (denoland#22216)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Feb 1, 2024
1 parent e58b190 commit 4f914dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 11 additions & 0 deletions cli/tests/unit/copy_file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
);
13 changes: 8 additions & 5 deletions ext/fs/std_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4f914dd

Please sign in to comment.