From d43cb12d2fe01047992dcd6ce92ca914ea21c571 Mon Sep 17 00:00:00 2001 From: Sebastien Tardif Date: Wed, 24 Jun 2026 09:06:06 -0700 Subject: [PATCH] std: reject interior NULs in Windows env::set_current_dir / chdir chdir built the wide path with encode_wide plus a trailing 0 only, so an interior NUL truncated SetCurrentDirectoryW. Route through to_u16s which rejects NULs and appends the terminator. Signed-off-by: Sebastien Tardif --- library/std/src/sys/paths/windows.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/std/src/sys/paths/windows.rs b/library/std/src/sys/paths/windows.rs index cfdc93847a97a..3fd402d7a84fc 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> { @@ -105,8 +105,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 before SetCurrentDirectoryW; an embedded 0 would + // truncate the path at the WinAPI boundary (same class as AF_UNIX / junction). + let p = to_u16s(p)?; cvt(unsafe { c::SetCurrentDirectoryW(p.as_ptr()) }).map(drop) }