std.os.windows: LPSTR
/LPCSTR
/LPWSTR
/LPCWSTR
/etc being sentinel-terminated is not always correct
#20858
Labels
bug
Observed behavior contradicts documented or intended behavior
os-windows
standard library
This issue involves writing Zig code for the standard library.
Milestone
Zig Version
0.14.0-dev.625+432905223
Steps to Reproduce and Observed Behavior
From https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/50e9ef83-d6fd-4e22-a34a-2c6b4e3c24f3:
Zig defines
LPWSTR
andLPCWSTR
and friends as sentinel-terminated:zig/lib/std/os/windows.zig
Lines 2850 to 2851 in 390c7d8
The sentinel was added to these types in 2662e50
This is not a huge deal, but it makes using APIs that take a buffer as input a bit awkward. Take
GetModuleFileNameW
, for example:That is, the result is null-terminated, but the buffer does not need to be. With Zig's definition of
LPWSTR
, though, the buffer itself needs to be sentinel-terminated, which also means that usingbuf.len
directly as the length is incorrect:Expected Behavior
This is a "problem" introduced by the Zig type system actually being able to encode a pointer being sentinel terminated, so I think there are a few plausible solutions here:
LPSTR
/LPCSTR
/LPWSTR
/LPCWSTR
/etc.LPSTR
/LPCSTR
/LPWSTR
/LPCWSTR
types entirely and replace all instances of them with the specific type itself (so, for example, allLPWSTR
instances would get changed to either[*]WCHAR
or[*:0]WCHAR
)The text was updated successfully, but these errors were encountered: