Skip to content

Windows chdir/set_current_dir does not reject interior NUL (silent truncation) #542

Description

@SebTardif

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libsStandard libraryI-wrongWrong result or data corruptionO-windowsWindows-specificP-lowLow impact: edge case or niche scenariobugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions