Bug
library/std/src/sys/paths/windows.rs chdir encodes the path with encode_wide() and appends a trailing 0, but does not scan for interior NUL wide units. SetCurrentDirectoryW stops at the first embedded zero, so env::set_current_dir with a path containing an interior \0 can succeed and switch to a truncated path instead of returning InvalidInput.
Other Windows entry points use to_u16s / ensure_no_nuls, which reject interior NULs.
Impact
Silent wrong working directory on pathological OsStr input (rare but violates the usual “no interior NUL” contract for WinAPI paths).
Origin
| Lines |
Commit |
Date |
Author |
| 106-111 (chdir without to_u16s) |
2d200c9c8bd |
2015-02-23 |
Alex Crichton |
Suggested fix
Use to_u16s(p)? then SetCurrentDirectoryW.
WSL note
Same code path on Windows hosts; WSL does not implement this in std (no wslpath helper in the standard library). Path/NUL bugs affect WSL2-interop only when the program runs as a Windows PE calling SetCurrentDirectoryW.
Bug
library/std/src/sys/paths/windows.rschdirencodes the path withencode_wide()and appends a trailing0, but does not scan for interior NUL wide units.SetCurrentDirectoryWstops at the first embedded zero, soenv::set_current_dirwith a path containing an interior\0can succeed and switch to a truncated path instead of returningInvalidInput.Other Windows entry points use
to_u16s/ensure_no_nuls, which reject interior NULs.Impact
Silent wrong working directory on pathological
OsStrinput (rare but violates the usual “no interior NUL” contract for WinAPI paths).Origin
2d200c9c8bdSuggested fix
Use
to_u16s(p)?thenSetCurrentDirectoryW.WSL note
Same code path on Windows hosts; WSL does not implement this in
std(nowslpathhelper in the standard library). Path/NUL bugs affect WSL2-interop only when the program runs as a Windows PE callingSetCurrentDirectoryW.