Skip to content

std::env::home_dir() is buggy on Windows #31502

Closed
@nodakai

Description

@nodakai

The underlying Win32 API GetUserProfileDirectory() is used in a wrong way:

It is the in/out parameter lpcchSize rather than the function return value that contains the number of bytes of the returned data. As a result, when neither HOME nor USERPROFILE is set, std::env::home_dir() returns a truncated string "C" when it should return "C:\Users\nodakai", for example.

I'd rather call this bug a result of a lack of idioms for robust interfacing with external C APIs than an instance of careless programming.

How do you like the idea of changing std::sys::c::BOOL to an enum with repr(i32) ? That should be a zero-cost abstraction and std::sys::c will remains a thin wrapper of Win32 API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions