From 46215391ecdc60d68f8fe5b2f31abf2293499cf2 Mon Sep 17 00:00:00 2001 From: Sebastien Tardif Date: Tue, 23 Jun 2026 20:59:05 -0700 Subject: [PATCH] std: reject interior NULs in Windows chdir Signed-off-by: Sebastien Tardif --- library/std/src/sys/paths/windows.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/library/std/src/sys/paths/windows.rs b/library/std/src/sys/paths/windows.rs index cfdc93847a97a..b3df6fd13ccce 100644 --- a/library/std/src/sys/paths/windows.rs +++ b/library/std/src/sys/paths/windows.rs @@ -8,7 +8,7 @@ use crate::os::windows::prelude::*; use crate::path::{self, PathBuf}; #[cfg(not(target_vendor = "uwp"))] use crate::sys::pal::api::WinError; -use crate::sys::pal::{api, c, cvt, fill_utf16_buf, os2path}; +use crate::sys::pal::{api, c, cvt, fill_utf16_buf, os2path, to_u16s}; use crate::{fmt, io, ptr}; pub struct SplitPaths<'a> { @@ -104,10 +104,9 @@ pub fn getcwd() -> io::Result { } pub fn chdir(p: &path::Path) -> io::Result<()> { - let p: &OsStr = p.as_ref(); - let mut p = p.encode_wide().collect::>(); - p.push(0); - + // Reject interior NULs (to_u16s) so SetCurrentDirectoryW cannot silently + // truncate at the first embedded zero code unit. + let p = to_u16s(p)?; cvt(unsafe { c::SetCurrentDirectoryW(p.as_ptr()) }).map(drop) }